单击按钮将重复的值添加到datagrid中的新列. [英] Adding a new Column to datagrid with repeating values on button click.

查看:74
本文介绍了单击按钮将重复的值添加到datagrid中的新列.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单中有一个数据网格.

除了要使用的数据源外,我还想在按钮click上添加其他列.这些列单元格在每一行中都包含相同的文本(字符串).

我知道如何添加列和textboxcell.我下面的代码很好地做到了.
但是我不知道如何在这个新创建的textboxcell中显示一些值.

没有用于分配值的``对象textboxcell .text ="EDR";''. 下面的代码.请帮助.
也需要帮助代码.谢谢.

(请注意,我只是想在datagrid上显示它.不需要更新数据库)

I have a datagrid in my form.

in addition to the datasource im using, i want to add additional columns on a button click.these coloumn cells contains same text (string) in every row.

i know how to add column and textboxcell. my below code does that well.
but i dont know how to show some values in this new created textboxcell .

there are no ''object textboxcell.text = "EDR";'' to assign values.
code below. pls help.
expecting helping codes too.thanks.

(pls note i just want to show this on datagrid. update to the database is not required)

        public void loadDataGrid(string sqlQueryString)
        {
            OleDbCommand SQLQuery = new OleDbCommand();
            DataTable data = null;
            dataGridView1.DataSource = null;
            SQLQuery.Connection = null;
            OleDbDataAdapter dataAdapter = null;
            dataGridView1.Columns.Clear();
            SQLQuery.CommandText = sqlQueryString;
            SQLQuery.Connection = conn;
            data = new DataTable();
            dataAdapter = new OleDbDataAdapter(SQLQuery);
            dataAdapter.Fill(data);
            dataGridView1.DataSource = data;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.ReadOnly = true;
          
            dataGridView1.Columns[0].Width = 20; 
            dataGridView1.Columns[1].Width = 90;
            dataGridView1.Columns[2].Width = 90;
            dataGridView1.Columns[3].Width = 90;
          }

         DataGridViewColumn EDR; // new column
         DataGridViewCell EDRcell; // textbox cell in column

         private void button1_Click(object sender, EventArgs e)
         {
            
         EDR = new DataGridViewColumn();
            EDRcell = new DataGridViewTextBoxCell();
            EDR.CellTemplate = EDRcell;
            EDR.HeaderText = "RedcordType";
            EDR.Name = "RedcordType";
            EDR.Visible = true;
            EDR.Width = 45;
            dataGridView1.Columns.Add(EDR);
            //is there something like 
 
            // EDRcell.text = "EDR"; ???
        }

推荐答案

foreach (GridViewRow row in dataGridView1.Rows)
{
row.Cells[row.Cells.Count-1].Text = "EDR";
}


您可以使用datagridview的复制方法轻松实现

You can easily do it with the copy method of datagridview

private void button1_Click(object sender, EventArgs e)
         {
         dataGridView1.Rows.AddCopies(0, 1); //row =0, no.of copies =1
        }


这篇关于单击按钮将重复的值添加到datagrid中的新列.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆