Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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)

C#
        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"; ???
        }
Posted
Updated 8-Aug-11 2:13am
v2
Comments
aslam.kship 8-Aug-11 9:16am    
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"; ???re>

what am im doing wrong here?.

why is EDRcell.Value = "EDR" not working here?

im a noob at this point. pls help!!

You can easily do it with the copy method of datagridview

C#
private void button1_Click(object sender, EventArgs e)
         {
         dataGridView1.Rows.AddCopies(0, 1); //row =0, no.of copies =1
        }
 
Share this answer
 
Comments
aslam.kship 8-Aug-11 8:47am    
gridview is already databound.
and does this code add row?
i need a new column with all cells text = "EDR".
foreach (GridViewRow row in dataGridView1.Rows)
{
row.Cells[row.Cells.Count-1].Text = "EDR";
}
 
Share this answer
 
Comments
aslam.kship 8-Aug-11 8:50am    
GridViewRow is missing. any additional namespace or referneces required?

hope this adds a new column with cells containing text = "EDR".thanks.
Herman<T>.Instance 8-Aug-11 9:16am    
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900