Thursday, February 4, 2010

How to use more than one DataKeyNames of a GridView in asp.net 2.0 / 3.5




Please visit my new Web Site WWW.Codedisplay.com



In my previous article i have described "how we can remove multiple GridView rows like gmail deletion at a time". This article is the continution article. In this article i will modify the base article class file to show you how one can use more than one datakeynames in a gridview as well as in editing time or in GridView manipulation time how one can read more than one datakeynames that you have assigned in design time or in runtime. The real example is let you have a product table. Which contains productid,brandid,category id as well. Also a product may have a different category. So when you show a list of products then you have to pick a product with productid, CategoryID for deletion or modification. Here i will describe how.

DataKeyNames is the property to define Read-only primary key or composite primary key like fields in a GridView control. We can also add some more fields to this property separated by commas.

At first have a look at the below example how to assign more than one or multiple datakeynames in a gridview:
<asp:GridView runat="server" ID="GridView1" DataKeyNames="ID,BrandID,CategoryID" AutoGenerateColumns="false">
<HeaderStyle BackColor="Red" Font-Bold="true" ForeColor="White" />
<RowStyle BackColor="Gray" />
<AlternatingRowStyle BackColor="LightGray" />
<Columns>
    <asp:TemplateField HeaderText="Select">
    <ItemTemplate>
    <asp:CheckBox runat="server" ID="chk"/>
    </ItemTemplate>
    <HeaderTemplate>
    <input id="chkAll" onclick="javascript:GridSelectAllColumn(this, 'chk');" runat="server" type="checkbox" value="" />
    </HeaderTemplate>
    </asp:TemplateField>

     <asp:BoundField DataField="Name" HeaderText="Name"/>
     <asp:BoundField DataField="Description" HeaderText="Description" />
     <asp:BoundField DataField="Color" HeaderText="Color" />
</Columns>
</asp:GridView>
Secondly read the below codes how you can read more than one or multiple datakeynames in manipulation time:
public bool PerformDelete(GridView GV, string sTableName)
    {
        bool bSaved = false;
        string sClause = "";
        string sSQL = "";
        string sConstr = "";
        SqlConnection Conn;
        SqlCommand comm;

        sConstr = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
        foreach (GridViewRow oItem in GV.Rows)
        {
            if (((CheckBox)oItem.FindControl("chk")).Checked)
            {
                for (int i = 0; i < GV.DataKeyNames.Length; i++)
                    sClause =sClause+" AND "+ GV.DataKeyNames.GetValue(i) + "=" + GV.DataKeys[oItem.DataItemIndex][i].ToString();

                sSQL = "DELETE FROM " + sTableName + " WHERE 1=1"+sClause;
                // The above sql will generate like the below query
                // DELETE FROM product WHERE 1=1 AND ID=4 AND BrandID=2 AND CategoryID=4
                Conn = new SqlConnection(sConstr);
                using (Conn)
                {
                    try
                    {
                        Conn.Open();
                        comm = new SqlCommand(sSQL, Conn);
                        using (comm)
                        {
                            comm.CommandTimeout = 0;
                            comm.ExecuteNonQuery();
                            bSaved = true;
                        }
                    }
                    catch (Exception Ex)
                    {
                        bSaved = false;
                        // You can through error from here.
                    }
                }
            }
        }
        return bSaved;
    }
Since this article is a continution of previous one so for better understanding you can read the base article first & then read this article. But if you need only know the use of multiple datakeynames then hope my above example code segments is enough for you.

1 comments:

sunil said...

It Nice

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash