开发者

ASP.NET Gridview Checkbox value to DataTable

开发者 https://www.devze.com 2022-12-26 16:00 出处:网络
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 (a

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 (a开发者_开发技巧s 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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消