DataGridView添加序号

方法一:直接法

using (SolidBrush b = new SolidBrush(DgvCMRMain.RowHeadersDefaultCellStyle.ForeColor))
            {
                e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
            }

效果图

方法二:DataTable新增一行

 以下经过试验,是完全正确的。

string strConn = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;
            SqlConnection cn = new SqlConnection(strConn);
            SqlDataAdapter da = new SqlDataAdapter("tbMan_hours_Show", cn);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.Add("@DDate", SqlDbType.VarChar);
            da.SelectCommand.Parameters.Add("@DeptID", SqlDbType.VarChar);
            da.SelectCommand.Parameters["@DDate"].Value = dtp_DDate.Value.ToShortDateString();
            da.SelectCommand.Parameters["@DeptID"].Value = strDeptID;
            DataSet ds = new DataSet();
            da.Fill(ds);

            System.Data.DataTable table =new DataTable();
            System.Data.DataColumn column = new DataColumn();
            column.ColumnName="序号";
            column.AutoIncrement=true;
            column.AutoIncrementSeed=1;
            column.AutoIncrementStep=1;
            table.Columns.Add(column);
            table.Merge(ds.Tables[0]);

            dgv_Manhours.DataSource = table;


效果图



阅读更多
文章标签: dataset table string
个人分类: WinForm(窗体c#)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭