1. 给DataGridView表格前面添加序号
//设置DataGridView表格前面的序号
private void ProDataView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
SolidBrush sbrush = new SolidBrush(Color.Black);
e.Graphics.DrawString((e.RowIndex + 1).ToString(),
e.InheritedRowStyle.Font,
sbrush,
e.RowBounds.Location.X + 10,
e.RowBounds.Location.Y + 5);
}2. 在线程中访问DataGridView
//设置DataGridView表格的数据源
private void SetDataGridViewSource(BindingSource bs)
{
if (this.ProDataView.InvokeRequired)
{
AddDataGridViewSource d = new AddDataGridViewSource(SetDataGridViewSource);
this.ProDataView.Invoke(d, new object[] { bs });
}
else
{
this.ProDataView.DataSource = bs;
}
}3. 其他一些小的应用
//返回DataGridView表格的总行数
private int ProDataView_GetRowCount()
{
return this.ProDataView.RowCount;
}
//返回DataGridVIew表格中的总列数
private int ProDataView_GetColumnCount()
{
return this.ProDataView.ColumnCount;
}
//返回DataGridView表格中指定位置的数据
private string ProDataView_GetCellDataText(int iRow, int iColumn)
{
return this.ProDataView.Rows[iRow].Cells[iColumn].Value.ToString();
}
使用C#在DataGridView中添加序号并操作数据
本文详细介绍了如何在DataGridView控件中添加序号,并提供了在线程中访问DataGridView的方法,同时展示了获取DataGridView表格的总行数、总列数以及指定位置的数据的技巧。
4668

被折叠的 条评论
为什么被折叠?



