C# DataGridView用法

方法三:对于ReadOnly的Display,有更为简便的方法 private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)         

 {             

 e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);       

   }  

DataGridView

DataGridView的几个基本操作:
1.获取某个(指定的)单元格的值:

dataGridView1.Row[i].Cells[j].Value;

2.获取选中的总行数:

dataGridView1.SelectedRows.Count;

3.获取当前选中行的索引:

dataGridView1.CurrentRow.Index;

4.获取当前选中单元格的值:

dataGridView1.CurrentCell.Value;

5.获取选中行的数据

复制代码

string[] str = new string[dataGridView.Rows.Count];
for(int i;i<dataGridView1.Rows.Count;i++)
{
    if(dataGridView1.Rows[i].Selected == true)
    {
        str[i] = dataGridView1.Rows[i].Cells[1].Value.ToString();
    }
}

复制代码

扫描二维码关注公众号,回复: 4765189 查看本文章

6.获取选中行的某个数据

int a = dataGridView1.CurrentRow.Index;
txtCardNo.Text = dataGridView1.Rows[a].Cells[0].Value.ToString();

7.获取某个(指定的)单元格的值:

dataGridView1.Rows[i].Cells[j].Value; Row[i] 

8.获取某行的索引值

int a=dataGridView1.CurrentRow.Index;

9.获取当前选中的行

selectedRows[0]

10.获取当前选择单元内容

DataGridView1.SelectedCells(0).Value.ToString 

11.获取当前选择单元第N列内容

DataGridView1.Rows(e.RowIndex).Cells(n).Value.ToString 

猜你喜欢

转载自blog.csdn.net/tmjianjun/article/details/82833504
今日推荐