我想通过更改c# .net中的属性来自动生成Datagridveiw标题中的行号,提前谢谢
发布于 2011-08-04 18:40:38
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>要使用c#生成行号事件,可以使用DataGridView的RowPostPaint事件
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
// get the row number in leading zero format,
// where the width of the number = the width of the maximum number
int RowNumWidth = dataGridView1.RowCount.ToString().Length;
StringBuilder RowNumber = new StringBuilder(RowNumWidth);
RowNumber.Append(e.RowIndex + 1);
while (RowNumber.Length < RowNumWidth)
RowNumber.Insert(0, "0");
// get the size of the row number string
SizeF Sz = e.Graphics.MeasureString(RowNumber.ToString(), this.Font);
// adjust the width of the column that contains the row header cells
if (dataGridView1.RowHeadersWidth < (int)(Sz.Width + 20))
dataGridView1.RowHeadersWidth = (int)(Sz.Width + 20);
// draw the row number
e.Graphics.DrawString(
RowNumber.ToString(),
this.Font,
SystemBrushes.ControlText,
e.RowBounds.Location.X + 15,
e.RowBounds.Location.Y + ((e.RowBounds.Height - Sz.Height) / 2));
}右键单击DataGridView,转到属性。然后你会得到一个如下所示的窗口。浏览属性顶部的事件,您可以看到几个可以由DataGridView.Select处理的事件,RowPostPaint并双击右侧将在代码后面创建一个事件。

发布于 2011-08-04 23:45:54
可以使用数据网格的CurrentRow属性
例如:我想改变背景颜色,如下所示:
myDataGrid.CurrentRow.DefaultCellStyle.BackColor = Color.Yellow;希望它对你有用,凯蒂。
https://stackoverflow.com/questions/6939471
复制相似问题