views:

471

answers:

4

I have a Gridview with values MachineGroupID, MachineGroupName, MachineGroupDesc and so on..

I have a delete command in the gridview, when I clicked is supposed to delete the selected row.

Now i have a code which tells me which row is selected.

 protected void LinkButton1_Click(object sender, EventArgs e)
 {
        LinkButton btn = (LinkButton)sender;
        GridViewRow row = (GridViewRow)btn.NamingContainer;
        string machineID = (String)GridView1.SelectedValue;
        if (row != null)
        {
            LinkButton LinkButton1 = (LinkButton)sender;

            // Get reference to the row that hold the button
            GridViewRow gvr = (GridViewRow)LinkButton1.NamingContainer;

            // Get row index from the row
            int rowIndex = gvr.RowIndex;
            string str = rowIndex.ToString();
            //string str = GridView1.DataKeys[row.RowIndex].Value.ToString();
            RemoveData(str); //call the delete method
       }
 }

I want to get the MachineGroupID present in the row so that i can pass that value and delete the record from the database and the gridview?

Thanks

+1  A: 

TRY THIS.........

((Label)grd.Rows[e.RowIndex].Cells[0].FindControl("lblID")).Text

OR best is RowCommand

protected void grdCountry_RowCommand(object sender, GridViewCommandEventArgs e)
{
    grd.DataKeys[0] // this will return the MachineGroupID
}
Muhammad Akhtar
thanks,,,, this worked..
I am astonished, after acceptance, why someone has downvoted and not write the reason as well?
Muhammad Akhtar
A: 

GridView has a SelectedDataKey property that will give you what you want (make sure that DataKeyNames property of the GridView is set to MachineGroupID)

roman m
A: 

Set MachineGroupID as DataKeyName for the grid as DataKeyName="MachineGroupID" and get it by

GridView1.DataKeys[rowIndex].ToString() as per your code.

Another way is set the MachineGroupID value to a hiddenfield in Gridview Itemtemplate and write the following code in codebehind in LinkButton1_Click event :

protected void LinkButton1_Click(object sender, EventArgs e)
 {
LinkButton btn = (LinkButton)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
string machineID = (String)GridView1.SelectedValue;
if (row != null)
{
LinkButton LinkButton1 = (LinkButton)sender;
// Get reference to the row that hold the button
GridViewRow gvr = (GridViewRow)LinkButton1.NamingContainer;
HiddenField hdn=(HiddenField)gvr.FindControl("hdn");
// get the value of MachineGroupID for the row.
string mcgID= hdn.value;

//--------- Write delete code here-------//
 }
 }
Himadri
A: 

There are a lot of ways to do this - here is one

I have an image button for deleting but you can use the linkbutton here

        <asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:ImageButton ImageUrl="~/Images/delete.gif" ID="btnDelete" OnClientClick='return confirm("Are you sure you want to delete this item?")' runat="server" OnCommand="DeleteRecord" CommandArgument='<%# Bind("MachineGroupId") %>' />
            </ItemTemplate>
        </asp:TemplateField>

And code:

protected void DeleteRecord(object sender, CommandEventArgs e)
{
    Guid id = new Guid((string)e.CommandArgument);
    if (RemoveData(id))
    {

    }
}
CRice
this is good... and i get the value of the machineGroupID in the e.CommandArgument.. but it gives an error when passing it to Guid id..The error msg is "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)."Is there a way to just give the string or int value to the id...??
Yeah, it was just an example. You don't have to use Guid unless your ID is a Guid. If it's supposed to be an int use int.Parse(), if its supposed to be a string just cast it (string)
CRice