龙空技术网

给datagridview添加自增列

逍遥总遥 464

前言:

而今看官们对“怎么在gridview后面添加一列”可能比较注意,小伙伴们都想要分析一些“怎么在gridview后面添加一列”的相关知识。那么小编也在网摘上汇集了一些关于“怎么在gridview后面添加一列””的相关内容,希望小伙伴们能喜欢,姐妹们快快来了解一下吧!

添加自增列代码如下:

using System.Data;using System.Data.Common;namespace mydgv{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            //绑定本来的数据            dataGridView1.DataSource = GetDT();            //创建一个按钮列 第4列            DataGridViewButtonColumn delbtn = new DataGridViewButtonColumn();            //是否用自定义的文本            delbtn.UseColumnTextForButtonValue = true;            delbtn.Text = "删除数据";            delbtn.Name = "Delete";            delbtn.HeaderText = "删除";            //添加按钮列            dataGridView1.Columns.Add(delbtn);            int count = dataGridView1.Rows.Count;            for (int i = 0; i < count - 1; i++)            {                //我这里是第2列格子的内容                string theid = dataGridView1.Rows[i].Cells[2].Value.ToString();                //如果内容是 1                 if (theid == "1")                {                    DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();                    dataGridView1.Rows[i].Cells[3] = txtcell;                    txtcell.ReadOnly = true;                }            }        }        //造几个数据 这里有3列        DataTable GetDT()        {            DataTable mydt = new DataTable();            #region 自增列            //第一列自增列            DataColumn zzl = mydt.Columns.Add("自增列", typeof(Int32));            //自动递增            zzl.AutoIncrement = true;            //值必须唯一            zzl.Unique = true;            //从多少开始,默认从0开始            zzl.AutoIncrementSeed = 1;            //每行间隔数为多少,也就是自增量            zzl.AutoIncrementStep = 10;            #endregion            //第二列ID            DataColumn dc0 = mydt.Columns.Add("ID", typeof(Int32));            //第三列数据            DataColumn dc1 = new DataColumn();            mydt.Columns.Add(dc1);            for (int i = 0; i < 5; i++)            {                DataRow newRow = mydt.NewRow();                newRow[1] = "0";                newRow[2] = i;                //在最后面插入数据                mydt.Rows.Add(newRow);            }            return mydt;        }        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)        {            //点吉的内容是哪一列            int CIndex = e.ColumnIndex;            //如果是第4列,也就是我们设置的按钮列            if (CIndex == 3)            {                //得到是第几行                int rowindex = e.RowIndex;                //得到该行其它格子的内容,                //我这里是第2列格子的内容                string theid = dataGridView1.Rows[rowindex].Cells[2].Value.ToString();                label1.Text = "准备删除!" + theid;            }        }    }}
效果如图一定要注意增加列后,各列的索引号变化。

添加功能代码及主要参数:

//第一列自增列

DataColumn zzl = mydt.Columns.Add("自增列", typeof(Int32));

//自动递增

zzl.AutoIncrement = true;

//值必须唯一

zzl.Unique = true;

//从多少开始,默认从0开始

zzl.AutoIncrementSeed = 1;

//每行间隔数为多少,也就是自增量

zzl.AutoIncrementStep = 10;

标签: #怎么在gridview后面添加一列