簡體   English   中英

在任何單元格中將字符串值與datagridview第0行進行比較

[英]Compare string value to datagridview row 0 in any cell

搜索此問題時找不到任何東西,因為我真的不知道該如何措辭。

我有一個dataGridView從Excel文件中獲取值。 效果很好,但我想將第0行中的所有值與字符串值進行比較。

這是我的代碼

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();
        }
}

我強烈建議您閱讀這篇文章

if(dataGridView1 != null)
{
    foreach (DataGridViewCell cell in dataGridView1.Rows[0].Cells)
    {
        if (cell.Value != null && cell.Value.Equals("test"))
        {
            tst.Text = cell.Value;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM