0

First off, im using a checkbox to highlight a true or false value from a dataset which I want to be able to change it. Unfortunately databinding the DataTable to the GridView checkboxes doesnt work (as you probably know) as it requires it to be in "edit" mode.

The way I've gotten round this is having the checkbox reading the data table value seperately rather than being databound and locked:

Checked='<%# Convert.ToBoolean(Eval("myValue")) %>'

So that solves checkbox values from DataTable -> GridView, now I need to do the opposite having the changed checkbox values go from GridView -> DataTable as it is not ready to submit it to my data source (SQL) just yet.

I figure I need to run a loop through the GridView rows, thats a give'un, what I can't figure out is how to replace/update just a single value in a row of a DataTable , only the ability to add or remove an entire row by the looks of things (something I don't want to do).

So can anyone offer a workaround? Or is there a way to have a databound but always editable checkbox in a gridview?

2 Answers 2

1

I've addressed the problem by starting from this article by Matt Dotson and the associated code: http://blogs.msdn.com/mattdotson/articles/490868.aspx

Basically its possible to make all the rows editable by default - in your case you'd want the majority of the columns to be read only. I think my first attempts just used the code pretty much as give, but my more recent attempts have been done with my own code based on the above.

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

Comments

1

dt.Rows[i].ItemArray[j] would do the trick :)

replace j with the index of the column

The ItemArray Array would contain all the items for the given row :)

Check this article on MSDN http://msdn.microsoft.com/en-us/library/system.data.datarow.itemarray.aspx

2 Comments

I've got this working all the way upto the point at which it needs to be changed. The indexes all turn out correct, thats half the battle, but I can't set a value to an ItemArray value. Can you provide an example of a value being set to an ItemArray?
dt.Rows[i].ItemArray[0] = "Hello"; dt.Rows[i].ItemArray[1] = myVariable; dt.Rows[i].ItemArray[2] = 5+myInt; dt.Rows[i].ItemArray[2] = txtMyValue.Text; Hope this helps :)

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.