How to pass 2 DataKeyNames when Deleting(using Image Button) Row from Gridview ASP.Net

Last Reply on May 28, 2014 05:19 AM By Azim

Posted on May 28, 2014 03:38 AM

HTML:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="G_Name, Param_ID" AutoGenerateColumns="False" Width="100%" PageSize="8" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging">

<Columns>

    <asp:BoundField HeaderText="Goal Name" DataField="G_Name" Visible="false"/>
    <asp:BoundField HeaderText="Param Id" DataField="Param_ID" Visible="false"/>
                
    <asp:TemplateField ItemStyle-Width="300" ItemStyle-CssClass="ColumnAlignment" HeaderText="Goal Function Name">
        <ItemTemplate>                
          <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# String.Format("~/Defalut2.aspx?Name={0}&Id={1}" , Eval("G_Name"), Eval("Param_ID")) %>'           Text='<%# Eval("G_Name") %>'></asp:HyperLink>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Delete">
        <ItemTemplate>
          <asp:ImageButton ID="lnkdelete" ImageUrl="~/Images/del.png" Width="20" Height="20" runat="server" OnClick="lnkdelete_Click"/>
        </ItemTemplate>
    </asp:TemplateField> 

 </Columns>
                             
</asp:GridView>

C#:

//code to bind Gridview
        protected void GridBind()
        {
            string query = "SELECT distinct Param_ID, G_Name FROM Table1 where DeletionDate is null";
            this.GridView1.DataSource = DataManager.DatabaseManager.GetOrCreateConnection(DataManager.DatabaseManager.ConnectionType.MySQL).GetData(query);
            GridView1.DataBind();
        }

        //code for Gridview row delete
        protected void lnkdelete_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton lnkbtn = sender as ImageButton;
            GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;

            //string name = GridView1.DataKeys[gvrow.RowIndex].Value["G_Name"].ToString();
            //string id = GridView1.DataKeys[gvrow.RowIndex].Value["Param_ID"].ToString();

            string CommandText = "UPDATE Table1 SET DeletionDate = SYSDATE() , DeletedBy = '" + username + "' WHERE Param_ID = '" + id + "' 
            and G_Name = '" + name + "' ";

            object r = new object();
            r = DataManager.DatabaseManager.GetOrCreateConnection(DataManager.DatabaseManager.ConnectionType.MySQL).ExecuteScalerCommand(CommandText, null);
            int result = Convert.ToInt32(r);
            
            if (result == 0)
            {
                GridBind();
            }
        }

Since I am using 2 DataKeyName is Gridview, so I have to pass both the DataKey while deleting Row from Gridview(using "Image Button" of Gridview).

I tried it using below lines:

string name = GridView1.DataKeys[gvrow.RowIndex].Value["G_Name"].ToString();
string id = GridView1.DataKeys[gvrow.RowIndex].Value["Param_ID"].ToString();

But its not working.

Please suggest the correct way.

Posted on May 28, 2014 05:19 AM Modified on on May 28, 2014 08:32 AM

Refer this code and check Id and name in delete event of ImageButton.

HTML

<form id="form1" runat="server">
<div>
    <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" DataKeyNames="Id,Name"
        HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
            <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
            <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
            <asp:TemplateField HeaderText="Delete">
                <ItemTemplate>
                    <asp:ImageButton ID="lnkdelete" ImageUrl="~/Images/del.png" Width="20" Height="20"
                        runat="server" OnClick="lnkdelete_Click" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>
</form>

Namespace

using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;

 C#

 

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("Country",typeof(string)) });
        dt.Rows.Add(1, "John Hammond", "United States");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

protected void lnkdelete_Click(object sender, EventArgs e)
{
    GridViewRow gvrow = (sender as ImageButton).NamingContainer as GridViewRow;
    IOrderedDictionary dataKeyNames = GridView1.DataKeys[gvrow.RowIndex].Values;    
    Dictionary<string, string> dataKeyName = new Dictionary<string, string>();
    foreach (DictionaryEntry de in dataKeyNames)
    {       
        dataKeyName.Add(de.Key.ToString(), de.Value.ToString());
    }
    string id = dataKeyName["Id"].ToString();
    string name = dataKeyName["Name"].ToString();
}