简体   繁体   English

如何使用变量在C#.net(MVC)中指定列名

[英]How do I use a variable to specify the column name in C#.net (MVC)

lets say i got a variable that holds a whole row, i know its 可以说我有一个保存整行的变量,我知道它

row.column = "value";

and my code is : 我的代码是:

public void editseatsnr(string seatPostion , int BusID)
    {
        var bus = db.Seats.Select( s => s.BusID).FirstOrDefault();

    }

seatPostion is a string with the column name and BusID is a primary key in the first column seatPostion是具有列名称的字符串,而BusID是第一列中的主键

and heres a picture of the table : 这是一张桌子的照片:

桌子图片

lets say i got "A2" in seatPostion , and bus is at second row, how to supposed to edit the cell ? 可以说我在seatPostion中获得了“ A2”,而公共汽车在第二排,应该如何编辑单元格?

if You Want To Set the DatagridView Column Name , You Can Use this Code : 如果要设置DatagridView列名,则可以使用以下代码:

 grid.Columns[0].HeaderText = "First Column"; 

Also You Can Give The Name Of Column Instead Of index for example : columns["name"] 您也可以给列名而不是索引,例如: columns["name"]

I row is of type System.Data.DataRow , then pulling the values from the row based on a variable that holds the name of the column would work like this: 我的row类型为System.Data.DataRow ,然后根据保存列名称的变量从行中提取值,如下所示:

row[myVariable] = "some value";

If you need to reference the values by a literal column name, then you would do something like this: 如果需要通过文字列名称引用值,则可以执行以下操作:

row["myColumn"] = "some value";

See the DataRow MDSN article for more info. 有关更多信息,请参见DataRow MDSN文章。

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

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