datagridview 控件左侧的固定列能显示出序号吗?
------------------------------------------------
我看到有的程序界面上,左侧固定列是可以显示序号的,但是我不知如何实现的。
左侧的固定列有点宽,而且有一个右箭头,所以我想让这一列能显示出递增的序号
请问如何实现呢?
我确实看到有的程序中用到这个控件,可以显示序列号呀。
------------------------------------------------
我看到有的程序界面上,左侧固定列是可以显示序号的,但是我不知如何实现的。
左侧的固定列有点宽,而且有一个右箭头,所以我想让这一列能显示出递增的序号
请问如何实现呢?
我确实看到有的程序中用到这个控件,可以显示序列号呀。
解决方案 »
- 小菜再次提问 求助 关于数据库的,,,,,
- C#中给控件注册事件后,如何传递参数~!
- 【在线等】帮我看看这个excel数据导入access的程序吧~!
- C# 进程问题
- 如何提高传输效率,关于实时传输
- winform 中 模式窗体关闭的问题 ,大家看看怎么解决。
- ThreadPool建立的线程池,怎么把里面的线程结束呢????? 在线等谢谢!!!!
- 怎么求已知点在 已知面上投影坐标(c#编程实现)
- 如何实现表对表数据的插入(使用ListView)
- 为了通过单击按钮弹出窗体二,在button的click事件里添加2句,结果出错,为什么?
- begininvoke和endinvoke参数讲解
- DataGridView行标
int rowNumber = 1;
foreach (DataGridViewRow row in DataGridView1.Rows)
{
row.HeaderCell.Value = rowNumber.ToString();
rowNumber++;
}
/code]
int rowNumber = 1;
foreach (DataGridViewRow row in DataGridView1.Rows)
{
row.HeaderCell.Value = rowNumber.ToString();
rowNumber++;
}
{
//添加行号
using (SolidBrush b = new SolidBrush(dataGridOnlineCustomer.RowHeadersDefaultCellStyle.ForeColor))
{
string linenum = e.RowIndex.ToString();
int linen = Convert.ToInt32(linenum) + 1;
string line = linen.ToString();
e.Graphics.DrawString(line, e.InheritedRowStyle.Font, b, e.RowBounds.Location.X, e.RowBounds.Location.Y + 5);
}
}
{
using(SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
{
int linen = 0;
linen = e.RowIndex + 1;
string line = linen.ToString();
e.Graphics.DrawString(line, e.InheritedRowStyle.Font, b, e.RowBounds.Location.X, e.RowBounds.Location.Y + 5);
SolidBrush B = new SolidBrush(Color.Red);
}
}
{
using (SolidBrush b = new SolidBrush(dataGridOnlineCustomer.RowHeadersDefaultCellStyle.ForeColor))
{
string linenum = e.RowIndex.ToString();
int linen = Convert.ToInt32(linenum) + 1;
string line = linen.ToString();
e.Graphics.DrawString(line, e.InheritedRowStyle.Font, b, e.RowBounds.Location.X, e.RowBounds.Location.Y + 5);
}}
我如何调用呢?这样调用不行,第二个参数必须要指定,可是第二个参数我如何输入呢?
dataGridView1_RowPostPaint(dataGridView1,null);
private void dataGridView1_RowPostPaint_1(object sender, DataGridViewRowPostPaintEventArgs e)
{
if (e.RowIndex >= dataGridView1.FirstDisplayedScrollingRowIndex)
{
using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
{
int linen = 0;
linen = e.RowIndex + 1;
string line = linen.ToString();
e.Graphics.DrawString(line, e.InheritedRowStyle.Font, b, e.RowBounds.Location.X, e.RowBounds.Location.Y + 5);
SolidBrush B = new SolidBrush(Color.Red);
}
}
}
谢,这样的代码可以实现加序号的功能,但是发现一个细节,不知能否解决,
就是行指示器(即那个向右的箭头),会与序号重合,不好看,
这个问题应该如何处理呢?
<HeaderStyle HorizontalAlign="Center" Wrap="False" Width="40px"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center" Width="40px"></ItemStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%#MainGrid.Items.Count +1%>' ID="Label4" NAME="Label1">
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
这样可以显示出行数很简单的
既然能画序号,背景自然也能画掉
先画背景,再画序号
private void dataGridView1_RowPostPaint_1(object sender, DataGridViewRowPostPaintEventArgs e)
{
Rectangle rowHeaderBounds = new Rectangle
(
2, e.RowBounds.Top,
this.dataGridView1.RowHeadersWidth-2, e.RowBounds.Height - 1
); using (Brush backbrush =
new SolidBrush(SystemColors.Control))
{
e.Graphics.FillRectangle(backbrush, rowHeaderBounds);
} if (e.RowIndex >= dataGridView1.FirstDisplayedScrollingRowIndex)
{
using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
{
int linen = 0;
linen = e.RowIndex + 1;
string line = linen.ToString();
e.Graphics.DrawString(line, e.InheritedRowStyle.Font, b, e.RowBounds.Location.X, e.RowBounds.Location.Y + 5);
SolidBrush B = new SolidBrush(Color.Red);
}
}
}Rectangle rowHeaderBounds
这个矩形区域,你自己可以再调整一下