http://dotnet.aspx.cc/ShowDetail.aspx?id=C0B53A2D-EF45-4E07-9C95-2849700F32A8

解决方案 »

  1.   

    那是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);
    }
    }
      

  2.   

    请问在winform中在怎么解决啊,谢谢
      

  3.   

    就是阿,大哥帮人帮到底,有没有winform的代码C#的
      

  4.   

    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++;
    }
    }
    }
      

  5.   

    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 序号, ID
    FROM userlist a
      

  6.   

    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
    {}
    }
      

  7.   

    基本思路,添加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++;
    }}