C# 从DataGridView列、c结尾的空行中获取值#
我正在使用一个应用程序,该应用程序允许我将一些行添加到我的DataGridView: 如您所见,最后一行为空,这是为了允许添加更多行。 好的,当我尝试获取所有值时,问题就出现了在“Codigo”列中:C# 从DataGridView列、c结尾的空行中获取值#,c#,datagridview,C#,Datagridview,我正在使用一个应用程序,该应用程序允许我将一些行添加到我的DataGridView: 如您所见,最后一行为空,这是为了允许添加更多行。 好的,当我尝试获取所有值时,问题就出现了在“Codigo”列中: (DB)335T1613 (DB)335T1642 (DB)335T1644 最后一行为空,因此没有值,我得到以下错误: 无法删除新的未确认行 这是我的代码,我正在尝试获取以下值: private void buttonAccept_Click(object sender, EventA
- (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=“False”
使用绑定列表绑定到DataGrid并从TableView获取所有行。代码中哪一行出现错误?如果允许用户“添加”行,则grids
AllowUserToAddress
属性设置为true
。如果此属性设置为true
,则无法删除“最后一行”新行。幸运的是,每一行都有一个名为IsNewRow
的布尔属性。因此,在通过网格行的foreach
循环中…添加对此“新行”的检查…if(!row.IsNewRow){…}代码>