繁体   English   中英

从 DataGridView 列中获取值,最后是空行 c#

[英]Get values from a DataGridView Column, empty row at the end c#

我正在使用一个应用程序,它允许我向我的DataGridView添加一些行:

在此处输入图片说明

如您所见,最后一行是空的,这是为了允许添加更多行。 好的,当我尝试获取“Codigo”列中的所有值时出现问题:

  • (DB)335T1613
  • (DB)335T1642
  • (DB)335T1644

最后一行是空的,所以没有值,我收到以下错误:

无法删除新的未确认行

这是我的代码,我正在尝试只是为了得到这些值:

private void buttonAccept_Click(object sender, EventArgs e)
    {
        /*if (dataGridViewTennetPaint.Rows(dataGridViewTennetPaint.Rows.Count - 1).IsNewRow)
        {
               dataGridViewTennetPaint.Rows.RemoveAt(dataGridViewTennetPaint.Rows.Count - 1);
    }*/
        dataGridViewTennetPaint.Rows.RemoveAt(dataGridViewTennetPaint.Rows.Count - 1);
        string data = string.Empty;
        int indexOfYourColumn = 0;
        int cont = 0;
        foreach (DataGridViewRow row in dataGridViewTennetPaint.Rows)
        {
            //if (!row.Cells[indexOfYourColumn].Equals(null) || row.Cells[indexOfYourColumn] != null || row.Cells[indexOfYourColumn].Value.ToString() != "" || row.Cells[indexOfYourColumn].Value.ToString().Equals("") || row.Cells[indexOfYourColumn].Equals("") || row.Cells[indexOfYourColumn].Value.ToString() != "")
            if (row.Cells[indexOfYourColumn].Value != System.DBNull.Value)
            {
                data = row.Cells[indexOfYourColumn].Value.ToString();
                cont++;
                System.Diagnostics.Debug.WriteLine("Data contiene ... " + data + " cont: " + cont);
            }
        }


    }

首先,您需要从 DataGrid 中删除最后一行,因为它不再使用。

请将 CanUserAddRows 设置为 false。

CanUserAddRows="假"

击球手使用BindingList 绑定到 DataGrid 并从 TableView 中获取所有行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM