Showing posts with label GridView. Show all posts
Showing posts with label GridView. Show all posts

Friday, November 18, 2011

Scroll to particular record in GridView

Hello Friends,
here is a tip to scroll the gridview to particular record.
the basic concept is html scrolling. to scroll at particular position create an anchor tag with name as address (eg. #ScrollPoint '# is must')

now redirect the page to the location using javascript call
window.location.href = "#ScrollPoint";

 
same thing can be achieved by anchorlink href
here is sample code

HTML Page
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return MoveTo();" />
    <div style="height: 100px; overflow: auto;">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="ID" SortExpression="ID">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <a name='<%# Eval("ID") %>'></a>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            </Columns>
        </asp:GridView>
    </div>  


<script type="text/javascript">
        function MoveTo() {
            window.location.href = "#" + document.getElementById('<%= TextBox2.ClientID %>').value;
            return false;
        }
    </script>

Code Behind
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
            }
        }

        private void BindControls()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            for (int i = 0; i < 200; i++)
            {
                AddRow(dt);    
            }


            GridView1.DataSource = dt;
            GridView1.DataBind();

        }

        private static void AddRow(DataTable dt)
        {
            DataRow dr = dt.NewRow();
            dr["ID"] = dt.Rows.Count + 1;
            dr["Name"] = "Sandeep" + dt.Rows.Count + 1;
            dt.Rows.Add(dr);
        }
    }
Happy Coding...

Monday, April 4, 2011

Fetch Data from Control Without Loop- Best Use of LINQ

Hi friends….

Many times I need to generate collection type Data from GridView or Repeater or any other control.. and in that case I used to loop through each rowitem of particular control..

here each row of repeater is listitem of LINQ query.

same way you can perform for all DataBound Controls like GridView, Repeater, DataList, DropDownlist, CheckboxList, Radiobuttonlist etc…

but after using LINQ, I am able to do that very easily

here is sample of Repeater control to Get List of data

List ctrls = (from item in rptctrlList.Items.Cast()
                                    where ((CheckBox)item.FindControl("chkctrl")).Checked
                                    select new ctrlInfo()
                                    {
                                        ctrlId = Convert.ToInt32(((HiddenField)item.FindControl("hdnctrlId")).Value),
                                        UserctrlId = Convert.ToInt64(((HiddenField)item.FindControl("hdnUserctrlId")).Value),
                                    }).ToList();


now I can pass XML to database from this List<ctrlInfo> using


 



var ctrlXML = from ctrl in ctrls
                          select new XElement("ctrl",
                          new XElement("UserId", ctrl.UserId),
                          new XElement("ctrlId", ctrl.ctrlId),
                          new XElement("UserctrlId", ctrl.UserctrlId));
            ctrlsXML.Add(ctrlXML);





Now…. You can pass the XML to Stored Procedure. and in Stored Procedure


using


SELECT 
c.value('ctrlId[1]','int') As ctrlId
, c.value('UserId[1]','BigInt') As UserId
, c.value('UserctrlId[1]','BigInt') As UserctrlId
FROM @ctrlIdListXML.nodes('/ctrls/ctrl')e(c)




you can have Table of selected records of Repeater control.


using merge statement you can perform operation.


 


Hope this Helps


All the Best

Monday, October 18, 2010

Select Multiple Checkbox with Shift key in Gridview by javascript

Hello Friends,
Recently i was just looking at my Gridview where i have a checkbox in each row.. now i need to select all Check box.. for that lots of material is available on net..
and all are enough so i am not going to deep in it
check out this… http://www.codeproject.com/KB/grid/GridCheckBox.aspx

but now my problem was that i wanted to select few rows but not all
let’s say in my gridview the number of records are around 100.. and i want to check the checkbox of first 35 records
in that case i had to click on checkbox of all 35 rows.
i thought, like all mail accounts like gmail, yahoo, live and in may more…
i should be able to select multiple records by shift key.
means select the first record and pressing shift key select the 35st record so all the records in between will be selected..
and for that i found lots of jquery or any other javascripts.
but here i have tried to make something my own and i am happy to make it..
First With Shift Secod with Shift Selected with shift
here is code

on Page

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="Id" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" 

onclick="CheckAllCheckbox(document.getElementById('ctl00_ContentPlaceHolder1_GridView1'),this,event);"
                        runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>







on code Behind


protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           DataTable dt = new DataTable();
           dt.Columns.Add("ID");
           dt.Columns.Add("Name");
           DataRow dr;
           for (int i = 1; i < 130; i++)
           {
               dr = dt.NewRow();
               dr["ID"] = i;
               dr["Name"] = "Record No" + i.ToString();
               dt.Rows.Add(dr);
           }
           GridView1.DataSource = dt;
           GridView1.DataBind();
       }
   }
Javascript
var lst = null;
        function CheckAllCheckbox(ctrl, sValue, e) {
            var chkName = sValue.id;
            var arlstr = sValue.id.split(ctrl.id);
            var id = arlstr[1].split('_')[1].substring(3);

            var ee = (window.event) ? event : e;
            if (ee.shiftKey) {
                if (lst != null) {
                    var First = 0, last = 0;
                    if (parseInt(lst) > parseInt(id)) {
                        First = parseInt(id);
                        last = parseInt(lst);
                    }
                    else {
                        First = parseInt(lst);
                        last = parseInt(id);
                    }

                    for (i = First; i < last; i++) {                        
                        var p = (i <= 9 ? '0' + i : i);
                        var ctrlnew = chkName.replace('ctl' + id, 'ctl' + p);                        
                        document.getElementById(ctrlnew).checked = sValue.checked;
                    }                    
                }
            }
            lst = id;
        }
Here is the Source to Download http://cid-552998535cb9985d.office.live.com/self.aspx/.Public/SelectWithShift.rar Thanks