views:

65

answers:

1

Hi, I'm pretty new to C# and asp.net so aplogies if this is a really stupid question.

I'm using a grid view to display a number of records from a database.

Each row has an Edit Button. When the button is clicked I want an ID to be passed back to a funtion in my .cs file. How do I bind the rowID to the Button field?

I've tired using a hyper link instead but this doens't seem to work because I'm posting back to the same page which already has a Permanter on the URL.

asp.net

<asp:GridView ID="gvAddresses" runat="server" onrowcommand="Edit_Row">
    <Columns>
        <asp:ButtonField runat="server" ButtonType="Button" Text="Edit">
    </Columns>
</asp:GridView>

c#

        int ImplantID = Convert.ToInt32(Request.QueryString["ImplantID"]);
        Session.Add("ImplantID", ImplantID);

        List<GetImplantDetails> DataObject = ImplantDetails(ImplantID);

        System.Data.DataSet DSImplant = new DataSet();
        System.Data.DataTable DTImplant = new DataTable("Implant");

        DSImplant.Tables.Add(DTImplant);

        DataColumn ColPostCode = new DataColumn();
        ColPostCode.ColumnName = "PostCode";
        ColPostCode.DataType = typeof(string);
        DTImplant.Columns.Add(ColPostCode);

        DataColumn ColConsigneeName = new DataColumn();
        ColConsigneeName.ColumnName = "Consignee Name";
        ColConsigneeName.DataType = typeof(string);
        DTImplant.Columns.Add(ColConsigneeName);

        DataColumn ColIsPrimaryAddress = new DataColumn();
        ColIsPrimaryAddress.ColumnName = "Primary";
        ColIsPrimaryAddress.DataType = typeof(int);
        DTImplant.Columns.Add(ColIsPrimaryAddress);

        DataColumn ColImplantCustomerDetailsID = new DataColumn();
        ColImplantCustomerDetailsID.ColumnName = "Implant ID";
        ColImplantCustomerDetailsID.DataType = typeof(int);
        DTImplant.Columns.Add(ColImplantCustomerDetailsID); 

        foreach (GetImplantDetails Object in DataObject)
        {

            DataRow DRImplant = DTImplant.NewRow();
            DRImplant["PostCode"] = Object.GetPostCode();
            DRImplant["Consignee Name"] = Object.GetConsigneeName();
            DRImplant["Primary"] = Object.GetIsPrimaryAddress();
            DRImplant["Implant ID"] = Object.GeTImplantCustomerDetailsID();
            DTImplant.Rows.Add(DRImplant); <--- this is what I need to be added to the button

        }

        gvAddresses.DataSource = DTImplant;
        gvAddresses.DataBind(); 
+1  A: 

In your Edit_Row method you can access the index of the row you are editing like:

int rowIndex = (int)e.CommandArgument;

Once you have that you can access the row directly and pull out any values you want:

GridViewRow row = gvTest.Rows[rowIndex];
int implantId = Int32.Parse(string row.Cells[3].Text);

Alternatively, you can also add the property DataKeyNames="Implant ID" to your GridView and access the id that way:

int implantId = Int32.Parse(gvTest.DataKeys[rowIndex]["Implant ID"].ToString());
Chris Pebble
Thank you! Sorry I'm not sure I explained the probelm correctly. That or I just don't fully understand your awnser.ImplantID is a field in the database. So I want to pull it out of the Database(which I can do) and then attach it to the Button so that when the Onclick is triggered I know which record Needs to be Edited.
flyersun