gpt4 book ai didi

c# - DataGridView 行循环和单元格数据访问

转载 作者:太空宇宙 更新时间:2023-11-03 11:29:10 25 4
gpt4 key购买 nike

我有一个从数据库填充的 DataGridView,我需要获取每一行并从中创建一个对象。我不知道该怎么做。

DataGridView 列:Name Price ProductId Condition MemberId Description

我的代码:

foreach(DataGridViewRow row in members_dg.rows)
{
foreach(DataGridViewCell cell in row.Cells)
{
// need to get info. This should show u what im looking for.
//string name = cell1.Text;
//string price = cell2.Text;
//etc
//Member member = new Member(name, price, ...);
}
}

最佳答案

也许是这个?

foreach(DataGridViewRow row in members_dg.rows)
{
// need to get info. This should show u what im looking for.
string name = row.Cells[0].Value.ToString(); // first column
string price = row.Cells[1].Value.ToString(); // second column
//etc
Member member = new Member(name, price, ...);
}

您不必遍历每个单元格,只需为您所在的行调用单元格索引即可。

关于c# - DataGridView 行循环和单元格数据访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8407447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com