简体   繁体   中英

How to modify datatable in C#

I have a datatable but I can't modify row value when I use below command.

Int64 quantity = Convert.ToInt64(txtQuantity.Text);
blOfferList_temp.Rows[RowNo - 1]["Quantity"] = quantity;

It is just possible when I use

tblOfferList_temp.Rows[RowNo - 1]["Quantity"] = 100;

And I really need the fist one . Is there any way to modify datatable using variables??

I want to explain more about my problem

first of all I have a datatable which I define like this

private DataTable tblOfferList_temp;

then in a method I create datatable like this

tblOfferList_temp = new DataTable();
tblOfferList_temp.Columns.Add("OfferID", typeof(string));
tblOfferList_temp.Columns.Add("RowNo", typeof(Int16));
tblOfferList_temp.Columns.Add("SMCode", typeof(string));
tblOfferList_temp.Columns.Add("SSCode", typeof(string));
tblOfferList_temp.Columns.Add("Quantity", typeof(Int64));
tblOfferList_temp.Columns.Add("TotalArea", typeof(Int64));
tblOfferList_temp.Columns.Add("TotalValue", typeof(Int64));

after calling this method in form_load I have to store data into it and then show and modify its data in case needed

the store and show method works fine

(I call store method with button next and show with button prev and also with next )

and modify method ,which I have problem with , is needed after showing.

I don't know how to explain better . after prev button controls show the prev row values and I need to modify them and store them in the datatable with next button click

I noticed a typo in the first example— blOfferList_temp instead of tblOfferList_temp .

Aside from that, the only difference between these two code snippets is that in the first one, you are assigning a value of type Int64 and in the second you are assigning an Int32 (the default for an unspecified numeric). If using just 100 works fine, try casting to an Int32 instead:

Int32 quantity = Convert.ToInt32(txtQuantity.Text);
blOfferList_temp.Rows[RowNo - 1]["Quantity"] = quantity;

If txtQuantity.Text has values in it that don't fit in an Int32 you will have to change your datatable to accept Int64 s instead, and that's a whole different story.

(I'd also recommend putting some error handling in case txtQuantity.Text doesn't contain a parseable string but of course that's very dependent on where that value is coming from.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM