C# 将字符串值与任何单元格中的datagridview行0进行比较
当我搜索这个问题时,我找不到任何东西,因为我真的不知道如何用词来表达它 我有一个dataGridView,它从excel文件中获取值。这很好,但我想将第0行中的所有值与字符串值进行比较 这是我的密码C# 将字符串值与任何单元格中的datagridview行0进行比较,c#,visual-studio,datagridview,C#,Visual Studio,Datagridview,当我搜索这个问题时,我找不到任何东西,因为我真的不知道如何用词来表达它 我有一个dataGridView,它从excel文件中获取值。这很好,但我想将第0行中的所有值与字符串值进行比较 这是我的密码 string mat = "test"; if(mat == dataGridView1[0,0].Value.ToString()) { tst.Text = dataGridView1[1,0].Value.ToString(); } 它
string mat = "test";
if(mat == dataGridView1[0,0].Value.ToString())
{
tst.Text = dataGridView1[1,0].Value.ToString();
}
它仅适用于第一个条目中的单元格。我需要扫描所有的值。我想应该是这样的:
string mat = "test";
int x = ???;
if(mat == dataGridView1[0,x].Value.ToString())
{
tst.Text = dataGridView1[1,x].Value.ToString();
}
这让我发疯,因为这是一件如此简单的事情,我知道这是可能的。否则,我将不得不复制和粘贴x等于1,2,3,等等,无论工作表有多少个值
如果您有任何建议,我们将不胜感激。您可以将您的验证置于一个循环中
string mat = "test";
for(int i=0;i<dataGridView1.Rows[0].Cells.Count;i++){
if(dataGridView1[0,i].Value != null && mat == dataGridView1[0,i].Value.ToString())
{
tst.Text = dataGridView1[1,i].Value.ToString();
}
}
string mat=“test”;
对于(int i=0;iwhy是您的循环,直到我只取了一个随机数为什么不使用实际变量(列数/单元格数)?dataGridView1.Rows[0]。cells.Count@varocarbas错过了,抱歉
if(dataGridView1 != null)
{
foreach (DataGridViewCell cell in dataGridView1.Rows[0].Cells)
{
if (cell.Value != null && cell.Value.Equals("test"))
{
tst.Text = cell.Value;
}
}
}