Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Is it at all posible to fill a dataGridView Column with say a textbox.text text?
 
So if i have a Column in my dataGridView named "Cost" and my textBox1.Text = "200"
 
It will fill the whole "Cost" Column all the Column Cells with = "200"?
 
This is how I get my Data for the DataGrid:
 

private void GetData(string selectCommand)
        {
            try
            {
 
                string conectioninfo = Properties.Settings.Default.finemanConnectionString;
                connection = new MySqlConnection(conectioninfo);
                da = new MySqlDataAdapter(selectCommand, connection);
                MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(da);
                dataGridView1.AutoGenerateColumns = false;
                DataTable table = new DataTable();
 

                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                da.Fill(table);
                binder.DataSource = table;
                dataGridView1.DataSource = this.binder;
                SetupColumns(table);
 

                }
 
Now The Column call Cost will not be in the BindingSource but still need to bee added into a seperate column called "Cost"
 
Any advice is appreciated.
 
Thanks
Posted 1-Sep-11 3:53am
Edited 1-Sep-11 4:15am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Insert a new column into your datatable, and for each row set the value to 0 (or whatever value is appropriate. THEN bind the database to the datagrid.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can insert a column into your table manually, and then set the value of each row like this:
 
table.Columns.Add("Cost", typeof(decimal));
            foreach (DataRow row in table.Rows)
            {
                row["Cost"] = 100;
            }
 

Hope this helps
  Permalink  
Comments
Dirk C De Winnaar at 1-Sep-11 10:13am
   
It's not working.
 
This is how I build my Columns
 
private void SetupColumns(DataTable table)
{
DataGridViewTextBoxColumn caseColumn = new DataGridViewTextBoxColumn();
caseColumn.Width = 140;
caseColumn.DataPropertyName = "case";
caseColumn.HeaderText = "Case No";
caseColumn.ValueType = typeof(string);
caseColumn.Frozen = true;
dataGridView1.Columns.Add(caseColumn);
 
DataGridViewTextBoxColumn RegNoColumn = new DataGridViewTextBoxColumn();
RegNoColumn.DataPropertyName = "regnum";
RegNoColumn.HeaderText = "Reg No";
RegNoColumn.Frozen = true;
RegNoColumn.ValueType = typeof(string);
dataGridView1.Columns.Add(RegNoColumn);
 
DataGridViewTextBoxColumn AreaColumn = new DataGridViewTextBoxColumn();
AreaColumn.DataPropertyName = "issuing_authority";
AreaColumn.HeaderText = "Area";
AreaColumn.Frozen = true;
AreaColumn.ValueType = typeof(string);
dataGridView1.Columns.Add(AreaColumn);
 

 
DataGridViewTextBoxColumn OffenceDateColumn = new DataGridViewTextBoxColumn();
OffenceDateColumn.DataPropertyName = "date_entered";
OffenceDateColumn.HeaderText = "Offence Date";
OffenceDateColumn.Frozen = true;
OffenceDateColumn.ValueType = typeof(DateTime);
dataGridView1.Columns.Add(OffenceDateColumn);
 
DataGridViewTextBoxColumn OriginalCostColumn = new DataGridViewTextBoxColumn();
OriginalCostColumn.Width = 90;
OriginalCostColumn.DataPropertyName = "amount";
OriginalCostColumn.HeaderText = "Original Cost";
OriginalCostColumn.Frozen = true;
OriginalCostColumn.ValueType = typeof(string);
dataGridView1.Columns.Add(OriginalCostColumn);
 
DataGridViewTextBoxColumn ReducedCostColumn = new DataGridViewTextBoxColumn();
ReducedCostColumn.DataPropertyName = "reduced_cost";
ReducedCostColumn.HeaderText = "Reduced Cost";
ReducedCostColumn.Frozen = true;
ReducedCostColumn.ValueType = typeof(string);
dataGridView1.Columns.Add(ReducedCostColumn);
 
DataGridViewTextBoxColumn StatusColumn = new DataGridViewTextBoxColumn();
StatusColumn.DataPropertyName = "status";
StatusColumn.HeaderText = "Status";
StatusColumn.Frozen = true;
StatusColumn.ValueType = typeof(string);
dataGridView1.Columns.Add(StatusColumn);
 
DataGridViewTextBoxColumn ServiceFeeColumn = new DataGridViewTextBoxColumn();
ServiceFeeColumn.Width = 80;
ServiceFeeColumn.DataPropertyName = "Cost";
ServiceFeeColumn.HeaderText = "Service Fee";
ServiceFeeColumn.Frozen = true;
ServiceFeeColumn.ValueType = typeof(string);
dataGridView1.Columns.Add(ServiceFeeColumn);
 

DataGridViewTextBoxColumn DescriptionColumn = new DataGridViewTextBoxColumn();
DescriptionColumn.Width = 165;
DescriptionColumn.DataPropertyName = "Description";
DescriptionColumn.HeaderText = "Description";
DescriptionColumn.Frozen = true;
DescriptionColumn.ValueType = typeof(string);
dataGridView1.Columns.Add(DescriptionColumn);
Dirk C De Winnaar at 1-Sep-11 10:15am
   
Don't worry! It's working. Your foreach worked 100%
 
Thank you very much!

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

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 BillWoodruff 173
3 Sergey Alexandrovich Kryukov 170
4 PIEBALDconsult 160
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 1 Sep 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100