【问题标题】:String is null or empty datagrid字符串为空或空数据网格
【发布时间】:2014-01-22 19:12:48
【问题描述】:

我希望我的数据网格的第一列不为空。所以我创建了这个检查字符串是否为空或空。

private void provjeri_unose()
    {
        string šifra = "šifra";
        for (int t = 0; t < dataGridView1.Rows.Count; t++)
        {
            šifra = Convert.ToString(dataGridView1.Rows[t].Cells[0].Value);
        }
        Proizvod.Šifra = šifra;
        if (string.IsNullOrEmpty(Proizvod.Šifra))
        {
            MessageBox.Show("Šifra mora biti unesena");
        }
    }

我确实在第一列的数据网格中输入了值,但它仍然显示消息框,只有在第一列中没有输入数据时才应该显示该消息框。 我的问题是,我在哪里做错了。

【问题讨论】:

  • 您的目标是哪种应用程序,我认为您的最后一行是空的,并且由于您正在迭代所有行,因此它显示了最后一行的单元格值。
  • + 您想确保网格的第一列中没有空单元格,还是要确保第一列第一行中的单元格不为空?跨度>
  • 我想你给了我想法,稍微更改了代码并在计数器中添加了 -1。现在有效!
  • 如果不为空,我希望它检查第一列中的每个单元格。现在它这样做了。

标签: c# datagridview datagrid


【解决方案1】:

工作代码:

 private void provjeri_unose()
    {
        string šifra = "šifra";
        for (int t = 0; t < dataGridView1.Rows.Count-1; t++)
        {
            DataGridViewRow row = dataGridView1.Rows[t];
            if (row.IsNewRow) break;
            šifra = Convert.ToString(dataGridView1.Rows[t].Cells[0].Value);
            Proizvod.Šifra = šifra;
            if (string.IsNullOrEmpty(Proizvod.Šifra))
            {
                MessageBox.Show("Šifra mora biti unesena");
            }
        }

    }

【讨论】:

  • 这不会检查最后一行的单元格。将for (int t = 0; t &lt; dataGridView1.Rows.Count-1; t++) 更改为for (int t = 0; t &lt; dataGridView1.Rows.Count; t++) 以使其工作
【解决方案2】:
            gridgetrequest.DataSource = dt;
            gridgetrequest.DataBind();

            string emptydata = dt.Rows[0]["processdate"].ToString();

            if (dt.Rows.Count > 0)
            {

                if (emptydata == "")
                {
                    foreach (GridViewRow row in gridgetrequest.Rows)
                    {
                        Label checkPRNNo = (Label)row.FindControl("lblprocessdate");

                        checkPRNNo.Text = "anand";
                    }



                }
            }

【讨论】:

    猜你喜欢
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 2021-09-22
    相关资源
    最近更新 更多