0

My problem is:

I have a GridView, which is bound to list of declared DTO objects with column of CheckBoxes. Also, I have a "Save" button on the page. My mission concludes in getting all rows, where CheckBox is checked and get the containing data. Unfortunately, row.DataItem = null on Button1_Click, because, I'm using if(!IsPostBack) gw.DataBind() statement (otherwise, I can't get CheckBox status). Could you give me the best practice for solving my problem?

Thank you in advance!

1 Answer 1

1

If you do if(!IsPostBack) --> gw.DataBind(), that will reinitialize your GridView and the checkboxes will be set to unchecked again.

In your case, in Button1_Click event, you can loop through each DataRow on your GridView and find the row which has it's checkbox checked and get all the selected row data.

foreach (GridViewRow gvRow in gw.Rows) {
// Find your checkbox
    CheckBox chkBox = (CheckBox)gvRow.FindControl("checkBox");

    if (chkBox.Checked) {
    // Get your values 
        string value1 = gvRow.Cells[1].Text;
        string value2 = gvRow.Cells[2].Text;
        // etc...
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

That's why I'm stuck. If I DataBind every time page loads, I can't get CheckBox values. If I use !IsPostBack -> row.DataItem == null ,so I can't get row's data.
Please, explain, how I'm supposed to get all the row data?
gvRow.Cells[1].ToString() gives me "System.Web.UI.WebControls.DataControlFieldCell", not the containing value.
I tried to use foreach(var row in GridView.Rows) var myObject=row.DataItem; But DataItem = null
It should be gvRow.Cells[1].Text, sorry abt that
|

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.