datagridview 左边设置一列 数字从小到大往下排列 请问,有没有可能做到Datagridview绑定数据的同时,我在左边也动态的生成相应的行数,且这列可被我赋值我是想做一个排名的程序,右边根据排序已经做出来,可是左边加不进去数字谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void DataGridView1_CellPainting(object sender,DataGridViewCellPaintingEventArgs e){ if (e.ColumnIndex < 0 && e.RowIndex >= 0) { e.Paint(e.ClipBounds, DataGridViewPaintParts.All); Rectangle indexRect = e.CellBounds; indexRect.Inflate(-2, -2); TextRenderer.DrawText(e.Graphics,(e.RowIndex + 1).ToString(),e.CellStyle.Font,indexRect,e.CellStyle.ForeColor,TextFormatFlags.Right | TextFormatFlags.VerticalCenter); e.Handled = true; }} private void DataGridView1_CellPainting(object sender,DataGridViewCellPaintingEventArgs e){ if (e.ColumnIndex < 0 && e.RowIndex >= 0) { e.Paint(e.ClipBounds, DataGridViewPaintParts.All); Rectangle indexRect = e.CellBounds; indexRect.Inflate(-2, -2); TextRenderer.DrawText(e.Graphics,(e.RowIndex + 1).ToString(),e.CellStyle.Font,indexRect,e.CellStyle.ForeColor,TextFormatFlags.Right | TextFormatFlags.VerticalCenter); e.Handled = true; }} 在DataGridView的RowPostPaint事件中添加以下代码: DataGridView DGV = sender as DataGridView; Rectangle rec = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, DGV.RowHeadersWidth - 4, e.RowBounds.Height); TextRender.DrawText(e.Graphics, (e.RowIndex+1).ToString(), DGV.RowHeadersDefaultCellStyle.Font, rec, DGV.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter|TextFormatFlags.Right); <asp:TemplateField HeaderText="编号"> <ItemStyle Width="5%" HorizontalAlign="Center" /> <ItemTemplate > <%# this.GridView1.PageIndex * this.GridView1.PageSize + Container.DataItemIndex + 1%> </ItemTemplate> </asp:TemplateField> 我们面的代码是经过测试的: private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { DataGridViewRow row; for (int i = 0; i < dataGridView1.Rows.Count; i++) { row = dataGridView1.Rows[i]; row.HeaderCell.Value = (i + 1).ToString(); } } SQL SERVER2005里有 ROW_NUMBER()可以帮你快速实现。 你难道不知道这是winform吗? 关于Gridview的问题!!急到要死! C# winform propertyGridControl细节的处理 C#开发linux下的webservice winform上传的问题 关于ProgressBar问题.下面是一个示例代码 WPF中的VisualBrush和Brush,这个问题郁闷死我了。。回答完毕立即给分,在线等... 各位大虾帮下小弟啊/ [求购]视频源代码。 winform有什么空件可以用来画表格 菜鸟的问题---熟悉C#和XML的大人过来 调用delphi的dll问题 C#读写XML的问题
{
if (e.ColumnIndex < 0 && e.RowIndex >= 0)
{ e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
Rectangle indexRect = e.CellBounds;
indexRect.Inflate(-2, -2);
TextRenderer.DrawText(e.Graphics,(e.RowIndex + 1).ToString(),e.CellStyle.Font,indexRect,e.CellStyle.ForeColor,TextFormatFlags.Right | TextFormatFlags.VerticalCenter);
e.Handled = true;
}
}
{
if (e.ColumnIndex < 0 && e.RowIndex >= 0)
{ e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
Rectangle indexRect = e.CellBounds;
indexRect.Inflate(-2, -2);
TextRenderer.DrawText(e.Graphics,(e.RowIndex + 1).ToString(),e.CellStyle.Font,indexRect,e.CellStyle.ForeColor,TextFormatFlags.Right | TextFormatFlags.VerticalCenter);
e.Handled = true;
}
}
DataGridView DGV = sender as DataGridView;
Rectangle rec = new Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
DGV.RowHeadersWidth - 4,
e.RowBounds.Height);
TextRender.DrawText(e.Graphics,
(e.RowIndex+1).ToString(),
DGV.RowHeadersDefaultCellStyle.Font,
rec,
DGV.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter|TextFormatFlags.Right);
<ItemStyle Width="5%" HorizontalAlign="Center" />
<ItemTemplate >
<%# this.GridView1.PageIndex * this.GridView1.PageSize + Container.DataItemIndex + 1%>
</ItemTemplate>
</asp:TemplateField>
{
DataGridViewRow row;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
row = dataGridView1.Rows[i];
row.HeaderCell.Value = (i + 1).ToString();
}
}
有 ROW_NUMBER()可以帮你快速实现。
你难道不知道这是winform吗?