简体   繁体   English

如何在C#中修改数据表

[英]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 在form_load中调用此方法后,我必须将数据存储到其中,然后在需要时显示和修改其数据

the store and show method works fine store和show方法工作正常

(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 . 在第一个示例中,我注意到了一个错字blOfferList_temp而不是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). 除此之外,这两个代码段之间唯一的区别是,在第一个中,您分配的是Int64类型的值,在第二个中,您分配的是Int32 (未指定数字的默认值)。 If using just 100 works fine, try casting to an Int32 instead: 如果仅使用100可以了,请尝试强制转换为Int32

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. 如果txtQuantity.Text包含的值不适合Int32 ,则必须更改数据表以接受Int64 ,这是txtQuantity.Text了。

(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.) (我还建议您进行一些错误处理,以防txtQuantity.Text不包含可分析的字符串,但是当然,这很大程度上取决于该值的来源。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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