简体   繁体   English

将新列添加到DataGridView并在运行时设置新值

[英]Add new column to DataGridView and Set new Value in run time

I have a field in my table with name (Kind) .it's type is int and, i give 0 and 1 values in app.now i want bind my datagridview with this table,and i want to add new columns (with this name:msg) to my datagridview ,that if kind=0 ,msg column (the cell value) will be "Manual", and if kind=1 ,msg column (the cell value) will be "Database" for example. 我的表中有一个名称为(Kind)的字段。它的类型是int,我在app中给出了0和1的值。现在我想将我的datagridview与该表绑定,并且我想添加新的列(具有以下名称: msg)到我的datagridview,例如,如果kind = 0,则msg列(单元格值)将为“手动”;如果kind = 1,则msg列(单元格值)将为“数据库”。 output of datagridview will be like: datagridview的输出将类似于:

      Kind       msg
   ----------------------- 
       0        Manual
       1       Database
       0        Manual

which instruction i should write to get this goal? 为了达到这个目标,我应该写哪一条指令? i mean how can i add a new column to datagridview and how can i set value to each cells? 我的意思是我如何向datagridview添加新列,以及如何为每个单元格设置值? thanks for your attention. 感谢您的关注。

If your data source is a System.Data.DataTable , then you could make use of expression columns . 如果您的数据源是System.Data.DataTable ,则可以使用表达式列

You'll have to decide whether that is an acceptable solution, and you'll also have to identify a suitable place within your code to add an expression column to your DataTable, but your code would look something like this: 您必须确定这是否是可接受的解决方案,并且还必须在代码中标识一个合适的位置,以便将表达式列添加到DataTable中,但是您的代码应如下所示:

dataTable.Columns.Add("msg", typeof(string), "IIF(Kind = 0, 'Manual', IIF(Kind = 1, 'Database', 'Unknown'))");

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

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