DataGrid问题,很小! http://dotnet.aspx.cc/ShowDetail.aspx?id=C0B53A2D-EF45-4E07-9C95-2849700F32A8 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 那是vb.net,可以转化一下嘛private void grid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if(e.Item.ItemIndex != -1) { e.Item.Cells[0].Text = Convert.ToString(e.Item.ItemIndex + 1); } } 请问在winform中在怎么解决啊,谢谢 就是阿,大哥帮人帮到底,有没有winform的代码C#的 protected void LineNo(DataTable dt,string field) { int s=1; foreach(DataRow row in dt.Rows ) { if(row.RowState!=DataRowState.Deleted) { if(int.Parse(row[field].ToString())!=s) { row[field]=s; } s++; } } } 1 在表头添加序号private void dataGridGoodsInfo_Paint(object sender, System.Windows.Forms.PaintEventArgs e){ int row =1; int y = 0; //取得总行数,我用的是强类型的 int count = this.dS_both.GoodsInfo.Rows.Count; while( row <= count) { string text = string.Format(" {0}", row); //dataGridGoodsInfo是绑定的dataGrid //顺序 y = this.dataGridGoodsInfo.GetCellBounds(row - 1, 0).Y + 2; //倒序 //y = this.dataGridGoodsInfo.GetCellBounds(count - row, 0).Y + 2; //dataGridGoodsInfo是绑定的dataGrid e.Graphics.DrawString(text,this.dataGridGoodsInfo.Font, new SolidBrush(Color.Red), 4, y); row ++; } }2 添加一列为序号(利用sql语句)--userlist数据表有ID列SELECT (SELECT COUNT(DISTINCT ID) FROM userlist--id <= a.ID 是从1开始,id < a.ID 是从0开始 WHERE id <= a.ID) AS 序号, IDFROM userlist a C#//重写DataGrid,添加行号 private void dataGrid1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { base.OnPaint(e); try { if(this.dataGrid1.DataSource!=null) { int yDelta; System.Drawing .Rectangle cell=this.dataGrid1.GetCellBounds(0,0); MessageBox.Show(this.dataGrid1.GetCellBounds(0,0).ToString()); int y=cell.Top +2; e.Graphics.DrawString("编号 ", this.Font, new SolidBrush(Color.Black), 8, y-18); //向DataGrid列头增加“编号” //判断DataGrid数据区是否有数据 if(this.dataGrid1.VisibleRowCount >0) //只在有记录集时在表格中显示序号 { CurrencyManager cm; cm = (CurrencyManager) this.BindingContext[this.dataGrid1.DataSource, this.dataGrid1.DataMember]; if(cm.Count >0) { int nRow=-1; y=41; //为第一行默认高度 while(nRow<0) { nRow=this.dataGrid1.HitTest (8,y).Row ; y++; } int nCount=0; while(y<this.Height && nCount<this.dataGrid1.VisibleRowCount ) { string text = string.Format("{0}", nRow+nCount+1); e.Graphics.DrawString(text, this.Font, new SolidBrush(Color.Black), 10, y); yDelta = this.dataGrid1.GetCellBounds( nRow+nCount,0).Height + 1;//****表示一行高度的参数 y += yDelta; //如果下面有子行显示序号的区分显示 if(this.dataGrid1.IsExpanded (nRow+nCount)&& nRow+nCount+1<cm.Count ) { y+=this.dataGrid1.GetCellBounds (nRow+nCount+1,0).Height +3; } nCount++; } } } } } catch {} } 基本思路,添加DataGrid.Paint事件在RowHeader上画行号http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q823q问题5.61上面有源代码,下面是我的部分代码:private void dataGrid2_Paint(object sender, PaintEventArgs e){ //定位0行,0列Cell的Position this.pointInCell00=new Point(this.dataGrid2.GetCellBounds(0,0).X+4,this.dataGrid2.GetCellBounds(0,0).Y+4); //首行 int row = this.dataGrid2.HitTest(this.pointInCell00).Row; //定义间隔 int yDelta = this.dataGrid2.GetCellBounds(row,0).Height+1; int y = this.dataGrid2.GetCellBounds(row,0).Top+2; while(y < this.dataGrid2.Height-yDelta && row < this.cmVovage.Count) { string rowText = string.Format("第{0}行",row+1); e.Graphics.DrawString(rowText,this.dataGrid2.Font,new SolidBrush(Color.Black),12,y); y+=yDelta; row++; }} C# 搜索并找到电脑中的某个文件 在一个解决方案下建立了多个项目,每个项目可不可以算做一个程序集? 比较两个dataset? 求个简单正则,谢谢网友了! 在被掉函数中获取调用类和调用方法名 注销变成了关机了! 如何测试网络连接,高分求助 请教:如何实现几个可选择用户都登录到一个窗体? 关于.Net类库中Boolean等数据值类型的问题 编写“个性化窗体”出现的问题(附源码) ◆■▲思归等高手进来讨论指点一下,对到对象比较的难题了。◆■▲ c#相对路径问题
private void grid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemIndex != -1)
{
e.Item.Cells[0].Text = Convert.ToString(e.Item.ItemIndex + 1);
}
}
{
int s=1;
foreach(DataRow row in dt.Rows )
{
if(row.RowState!=DataRowState.Deleted)
{
if(int.Parse(row[field].ToString())!=s)
{
row[field]=s;
}
s++;
}
}
}
private void dataGridGoodsInfo_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
int row =1;
int y = 0;
//取得总行数,我用的是强类型的
int count = this.dS_both.GoodsInfo.Rows.Count;
while( row <= count)
{
string text = string.Format(" {0}", row);
//dataGridGoodsInfo是绑定的dataGrid
//顺序
y = this.dataGridGoodsInfo.GetCellBounds(row - 1, 0).Y + 2;
//倒序
//y = this.dataGridGoodsInfo.GetCellBounds(count - row, 0).Y + 2;
//dataGridGoodsInfo是绑定的dataGrid
e.Graphics.DrawString(text,this.dataGridGoodsInfo.Font, new SolidBrush(Color.Red), 4, y);
row ++;
}
}
2 添加一列为序号(利用sql语句)
--userlist数据表有ID列
SELECT (SELECT COUNT(DISTINCT ID)
FROM userlist
--id <= a.ID 是从1开始,id < a.ID 是从0开始
WHERE id <= a.ID) AS 序号, ID
FROM userlist a
private void dataGrid1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
try
{
if(this.dataGrid1.DataSource!=null)
{
int yDelta;
System.Drawing .Rectangle cell=this.dataGrid1.GetCellBounds(0,0);
MessageBox.Show(this.dataGrid1.GetCellBounds(0,0).ToString());
int y=cell.Top +2;
e.Graphics.DrawString("编号 ", this.Font, new SolidBrush(Color.Black), 8, y-18); //向DataGrid列头增加“编号”
//判断DataGrid数据区是否有数据
if(this.dataGrid1.VisibleRowCount >0) //只在有记录集时在表格中显示序号
{
CurrencyManager cm;
cm = (CurrencyManager) this.BindingContext[this.dataGrid1.DataSource, this.dataGrid1.DataMember];
if(cm.Count >0)
{
int nRow=-1;
y=41; //为第一行默认高度
while(nRow<0)
{
nRow=this.dataGrid1.HitTest (8,y).Row ;
y++;
}
int nCount=0;
while(y<this.Height && nCount<this.dataGrid1.VisibleRowCount )
{
string text = string.Format("{0}", nRow+nCount+1);
e.Graphics.DrawString(text, this.Font, new SolidBrush(Color.Black), 10, y);
yDelta = this.dataGrid1.GetCellBounds( nRow+nCount,0).Height + 1;//****表示一行高度的参数
y += yDelta; //如果下面有子行显示序号的区分显示
if(this.dataGrid1.IsExpanded (nRow+nCount)&& nRow+nCount+1<cm.Count )
{
y+=this.dataGrid1.GetCellBounds (nRow+nCount+1,0).Height +3;
}
nCount++;
}
}
}
}
}
catch
{}
}
在RowHeader上画行号http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q823q
问题5.61
上面有源代码,下面是我的部分代码:private void dataGrid2_Paint(object sender, PaintEventArgs e)
{ //定位0行,0列Cell的Position
this.pointInCell00=new Point(this.dataGrid2.GetCellBounds(0,0).X+4,this.dataGrid2.GetCellBounds(0,0).Y+4);
//首行
int row = this.dataGrid2.HitTest(this.pointInCell00).Row;
//定义间隔
int yDelta = this.dataGrid2.GetCellBounds(row,0).Height+1;
int y = this.dataGrid2.GetCellBounds(row,0).Top+2;
while(y < this.dataGrid2.Height-yDelta && row < this.cmVovage.Count)
{
string rowText = string.Format("第{0}行",row+1);
e.Graphics.DrawString(rowText,this.dataGrid2.Font,new SolidBrush(Color.Black),12,y);
y+=yDelta;
row++;
}}