2017-08-22 59 views
0

在标准方法DataBindingComplete我遍历行/喜欢的cols为:如何在这种情况下获取单元名称?

for (int row = 0; row < dataGridView1.Rows.Count; row++) 
{ 
    for (int column = 0; column < dataGridView1.Columns.Count; column++) 
    { 
     object value = dataGridView1[column, row].Value; 
     if (value != null && value.GetType() == typeof(string)) 
     { 
      (...) 
     } 
    } 
} 

如何检查单元名称等作为?

我试着这样做:

if (dataGridView1[column, row].Name == "name") 
{ 
} 

编辑1 当我使用CellFormatting那么所有细胞开始横向滚动跳后。似乎渲染有问题。看图片。

enter image description here

+0

你的意思是列名吗? – Sach

+0

你尝试过'dataGridView1.Colums [column] .Name'。我认为这是行得通的。 –

+0

是的,列名 – OPV

回答

1

线CellFormatting事件,并尝试改变单元格的值下面的代码。

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "dataGridViewTextBoxColumn45") 
     e.Value = "oleg"; 
} 
+0

我不能使用cellFormatting,因为当我滚动datagrid它是坏的。渲染不起作用。 – OPV

+0

看看有问题的图片,当我使用格式化时,会发生什么,所有单元格跳转 – OPV

+0

我看不到任何图片... –