积极答复者
想请教有关于FirstOrDefault 在datagridview 的error

问题
-
我篇写了有关控制single 控制 datagridview columns的代码。但是,一直出现FirstOrDefault的error。想请各位指教指教
以下是我篇写的代码请查看
谢谢
private void Control_DGV_Column1()
{
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
Dictionary<string, int> dic = new Dictionary<string, int>();
dic.Add("ID", 40);
dic.Add("ProductID", 80);
dic.Add("ProductName", 90);
dic.Add("VendorName", 80);
dic.Add("Qty", 40);
dic.Add("ProductPrice", 80);
dic.Add("Date", 100);
dic.Add("Description", 150);
dic.Add("TotalPrice", 80);
for (int i = 0; i <= DataGridView1.Columns.Count - 1; i++)
{
int s = dic.Where(x => x.Key == DataGridView1.Columns[i].Name).Select(y => y.Value).FirstOrDefault;
DataGridView1.Columns[i].Width = s;
}
}
答案
-
Hi christing,
需要注意C#和 VB.NET 代码规范的区别。
在 C#中, FirstOrDefault是方法,所有方法必须加上():
int s = dic.Where(x => x.Key == DataGridView1.Columns[i].Name).Select(y => y.Value).FirstOrDefault();
在 VB.NET 中,你可以
Dim s As Integer = dic.Where(Function(x) x.Key = DataGridView1.Columns(i).Name).Select(Function(y) y.Value).FirstOrDefault()
也可以
Dim s As Integer = dic.Where(Function(x) x.Key = DataGridView1.Columns(i).Name).Select(Function(y) y.Value).FirstOrDefault
都是正确的。
Best Regards,
Xingyu Zhao
Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).
- 已建议为答案 Xingyu ZhaoMicrosoft contingent staff, Moderator 2021年5月5日 1:32
- 已标记为答案 christing 2021年5月10日 1:40
全部回复
-
Hi christing,
需要注意C#和 VB.NET 代码规范的区别。
在 C#中, FirstOrDefault是方法,所有方法必须加上():
int s = dic.Where(x => x.Key == DataGridView1.Columns[i].Name).Select(y => y.Value).FirstOrDefault();
在 VB.NET 中,你可以
Dim s As Integer = dic.Where(Function(x) x.Key = DataGridView1.Columns(i).Name).Select(Function(y) y.Value).FirstOrDefault()
也可以
Dim s As Integer = dic.Where(Function(x) x.Key = DataGridView1.Columns(i).Name).Select(Function(y) y.Value).FirstOrDefault
都是正确的。
Best Regards,
Xingyu Zhao
Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).
- 已建议为答案 Xingyu ZhaoMicrosoft contingent staff, Moderator 2021年5月5日 1:32
- 已标记为答案 christing 2021年5月10日 1:40
-
Hi christing,
如果问题已经得到解决,请将正确的回复标记为答案,这将有助于帮助其他遇到相似问题的人快速找到解决方案.
Best Regards,
Xingyu Zhao
Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).