0

I have been unable to figure this problem out. I used a SqlDataReader to generate data from my database into a grid view. I have been trying to generate buttons on each row, which I want to use to update the database.

I have been unable to get any values from the table to attach to the buttons. I am hoping to bind the ID but nothing I try works.

Here is my code for the original data binding query:

using (SqlConnection con = new SqlConnection(constr))
{
    con.Open();

    SqlCommand cmd = new SqlCommand("SELECT ID, Width, Length, DockID from Slip where ID not in (select slipID from lease) and dockID = @dockId", con);
    cmd.Parameters.AddWithValue("@dockId", dockId);

    SqlDataReader dr = cmd.ExecuteReader();
    gridView.DataSource = dr;
    gridView.DataBind();

    con.Close();
}

This is my attempt to generate buttons on each column, which work but I am unable to get any values from the table to bind to the buttons.

foreach (TableRow row in gvAvailableLeaseSlips.Rows)
{
    TableCell btnCell = new TableCell();
    Button btn = new Button();

    btn.Text = "Lease Slip";
    btn.CssClass = "btn";
    btn.Click += new EventHandler(BtnLease_Click);

    btnCell.Controls.Add(btn);

    row.Cells.Add(btnCell);
}

3 Answers 3

1

You cannot bind a grid from a datareader like this. Change the code like

        DataTable dt = new DataTable();
        dt.Columns.Add("dockId");
        dt.Columns.Add("width");
        dt.Columns.Add("length");


        while (obj_Reader.Read())
        {
            DataRow row = dt.NewRow();
            row["dockId"] = obj_Reader["dockId"];
            row["width"] = obj_Reader["width"];
            row["lenght"] = obj_Reader["LName"];
            dt.Rows.Add(row);
        }
        gridView.DataSource = dt;

or you can use a dataset to fill the gridview

Also, in gridview, you can autogenerate edit button to update values In gridview properties add AutoGnerateEditButton=true

Sign up to request clarification or add additional context in comments.

1 Comment

This helped me a lot. Thank you for this.
0

That's not how it is done. GridView has a DataKeys property which should be set to "ID" in your markup. Then in the click event handler for buttons, which seems to be present in each row according to your description, you should look for the corresponding DataRow and ask for its DataKey which will have your ID as an object.

1 Comment

P.S.: It has been many years since I have worked on ASP.NET, exact details may vary slightly but the process should be like what I wrote.
0

after set Datakeys property for your GridView, It would be :

int rowIndex = gvAvailableLeaseSlips.RowIndex;
int ID = (int)gvAvailableLeaseSlips.DataKeys[rowIndex]["yourKey"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.