简体   繁体   中英

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. output of datagridview will be like:

      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? thanks for your attention.

If your data source is a System.Data.DataTable , then you could make use of expression columns .

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.Columns.Add("msg", typeof(string), "IIF(Kind = 0, 'Manual', IIF(Kind = 1, 'Database', 'Unknown'))");

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