【问题标题】:How to show serial no. in data grid view in C# windows forms如何显示序列号。在 C# windows 窗体中的数据网格视图中
【发布时间】:2012-01-02 07:31:01
【问题描述】:

我有一个数据网格视图,我将它设置为数据源属性
作为

dgvEmployee.DataSource = mycollection;

我希望该网格应该具有一些属性,例如

dgvEmployee.ShowSerialNumber = True;

任何建议

提前致谢

【问题讨论】:

    标签: c# .net c#-3.0 c#-2.0


    【解决方案1】:

    这段代码取自here

        /// This class extends the the DataGridView so row numbers will 
        /// automatically appear in the row header cells. In this 
        /// implementation, the width of the column that contains the row 
        /// header cells is automatically adjusted to accomodate the row 
        /// numbering.
        /// ******************************************************************
        ///  AUTHOR: Daniel S. Soper
        ///     URL: http://www.danielsoper.com
        ///    DATE: 20 February 2007
        /// LICENSE: Public Domain. Enjoy!   :-)
        /// ******************************************************************
        /// 
        class MyDGV : DataGridView
        {
            public MyDGV()
            {
                //perform any necessary customization initialization here
            } //end default constructor
    
            protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
            { //this method overrides the DataGridView's RowPostPaint event 
              //in order to automatically draw numbers on the row header cells
              //and to automatically adjust the width of the column containing
              //the row header cells so that it can accommodate the new row
              //numbers,
    
                //store a string representation of the row number in 'strRowNumber'
                string strRowNumber = (e.RowIndex + 1).ToString();
    
                //prepend leading zeros to the string if necessary to improve
                //appearance. For example, if there are ten rows in the grid,
                //row seven will be numbered as "07" instead of "7". Similarly, if 
                //there are 100 rows in the grid, row seven will be numbered as "007".
                while (strRowNumber.Length < this.RowCount.ToString().Length) strRowNumber = "0" + strRowNumber;
    
                //determine the display size of the row number string using
                //the DataGridView's current font.
                SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
    
                //adjust the width of the column that contains the row header cells 
                //if necessary
                if (this.RowHeadersWidth < (int)(size.Width + 20)) this.RowHeadersWidth = (int)(size.Width + 20);
    
                //this brush will be used to draw the row number string on the
                //row header cell using the system's current ControlText color
                Brush b = SystemBrushes.ControlText;
    
                //draw the row number string on the current row header cell using
                //the brush defined above and the DataGridView's default font
                e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
    
                //call the base object's OnRowPostPaint method
                base.OnRowPostPaint(e);
            } //end OnRowPostPaint method
        } //end class
    

    【讨论】:

    • 我希望它应该可以工作,因为它是 datagridview 的内置属性
    【解决方案2】:
    //1-add new column to  dataGridView1 call it ser or your Name
    //2-set it's DataPropertyName To None "Very Important"
    //3-Set HeaderText to ("Your Name To shown In Header")
    //4-Set frozen To True 
    //5-in DefultCellStyle Do Thise
    //  A-Set BackColor To AnyColor you Want
    //  b-Set Null Value to 0 or 1 as you prefer
    //  b-Set Alignment To middleCenter
    //6- call Method MakeSerieal() everyTime You Want To fill dataGridView1
    //7- call Method MakeSerieal() In ColumnHeaderMouseClick ("It help if you Want To Reorder dataGridView1")
    
    //--------------------------
    private void MakeSerieal()
    {
    
      if (dataGridView1.Rows.Count > 1)
                            {
                                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                                {
                                    dataGridView1.Rows[i].Cells[0].Value = (i + 1).ToString();
                                }
                            }
    }
    //--------------------------
    
    //With My Regards 
    //Ahmed Mansour ("Egypt")
    //Mansour_VbKing@Yahoo>com
    

    【讨论】:

      【解决方案3】:

      在数据网格中添加新的模板字段,

      在模板字段内添加标签..

      在 datgrid 的 rowdatabound 事件中设置标签文本为序号。 http://bytes.com/topic/c-sharp/answers/874915-how-generate-serial-number-datagrid-view-automatically

      【讨论】:

        【解决方案4】:

        子类化网格并添加它。

        class MyGrid : DataGridView
        {
            public bool ShowSerialNumber
            {
                 get ...
                 set ...
            }
        }
        

        【讨论】:

          【解决方案5】:

          试试这个...不确定但试试这个。

          从数据库中填充一个数据表并将新列添加到数据表中

           DataColumn dcAuto = new DataColumn();
            dcAuto.AutoIncrement = true;
            dcAuto.AutoIncrementSeed = 1;
            dt.Columns.Add(dcAuto);
          

          将数据表绑定到gridview。

          【讨论】:

            【解决方案6】:

            试试这些绝对有效:

                        SqlDataAdapter sda0 = new SqlDataAdapter(sqlCmd0);
                        DataTable Dt0 = new DataTable();
                        Dt0.Columns.Add("SlNo");
                        Dt0.Columns["SlNo"].AutoIncrement = true;
                        Dt0.Columns["SlNo"].AutoIncrementSeed = 1;
                        sda0.Fill(Dt0);
                        grdName.DataSource = Dt0;
            

            【讨论】:

              【解决方案7】:

              这就像任何人都需要将序列号添加到他们的 datgridview 并且您希望序列号像 Microsoft word 中的项目符号和数字一样:- 这就是答案。您可以在 datagridview 属性“RowsAdded”和“RowsRemoved”中调用此方法

              //improvised answer of  ahmed mansour 
              
              private void MakeSerieal()
              {
                 if (dataGridView1.Rows.Count > 0)
                  {
                     int slno = 0;
                     for (int i = 0; i < dataGridView1.Rows.Count; i++)
                       { 
                         slno= slno+1;
                         dataGridView1.Rows[i].Cells[0].Value = slno.ToString();
                       }
                  }
               }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2022-01-08
                • 2017-05-17
                相关资源
                最近更新 更多