Click here to Skip to main content
11,921,889 members (58,031 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# 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)
                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;
                binder.DataSource = table;
                dataGridView1.DataSource = this.binder;


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.

Posted 1-Sep-11 3:53am
Edited 1-Sep-11 4:15am
Rate this: bad
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.
Rate this: bad
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
Dirk C De Winnaar 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;

DataGridViewTextBoxColumn RegNoColumn = new DataGridViewTextBoxColumn();
RegNoColumn.DataPropertyName = "regnum";
RegNoColumn.HeaderText = "Reg No";
RegNoColumn.Frozen = true;
RegNoColumn.ValueType = typeof(string);

DataGridViewTextBoxColumn AreaColumn = new DataGridViewTextBoxColumn();
AreaColumn.DataPropertyName = "issuing_authority";
AreaColumn.HeaderText = "Area";
AreaColumn.Frozen = true;
AreaColumn.ValueType = typeof(string);

DataGridViewTextBoxColumn OffenceDateColumn = new DataGridViewTextBoxColumn();
OffenceDateColumn.DataPropertyName = "date_entered";
OffenceDateColumn.HeaderText = "Offence Date";
OffenceDateColumn.Frozen = true;
OffenceDateColumn.ValueType = typeof(DateTime);

DataGridViewTextBoxColumn OriginalCostColumn = new DataGridViewTextBoxColumn();
OriginalCostColumn.Width = 90;
OriginalCostColumn.DataPropertyName = "amount";
OriginalCostColumn.HeaderText = "Original Cost";
OriginalCostColumn.Frozen = true;
OriginalCostColumn.ValueType = typeof(string);

DataGridViewTextBoxColumn ReducedCostColumn = new DataGridViewTextBoxColumn();
ReducedCostColumn.DataPropertyName = "reduced_cost";
ReducedCostColumn.HeaderText = "Reduced Cost";
ReducedCostColumn.Frozen = true;
ReducedCostColumn.ValueType = typeof(string);

DataGridViewTextBoxColumn StatusColumn = new DataGridViewTextBoxColumn();
StatusColumn.DataPropertyName = "status";
StatusColumn.HeaderText = "Status";
StatusColumn.Frozen = true;
StatusColumn.ValueType = typeof(string);

DataGridViewTextBoxColumn ServiceFeeColumn = new DataGridViewTextBoxColumn();
ServiceFeeColumn.Width = 80;
ServiceFeeColumn.DataPropertyName = "Cost";
ServiceFeeColumn.HeaderText = "Service Fee";
ServiceFeeColumn.Frozen = true;
ServiceFeeColumn.ValueType = typeof(string);

DataGridViewTextBoxColumn DescriptionColumn = new DataGridViewTextBoxColumn();
DescriptionColumn.Width = 165;
DescriptionColumn.DataPropertyName = "Description";
DescriptionColumn.HeaderText = "Description";
DescriptionColumn.Frozen = true;
DescriptionColumn.ValueType = typeof(string);
Dirk C De Winnaar 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 568
1 Dave Kreskowiak 285
2 Sergey Alexandrovich Kryukov 173
3 Richard MacCutchan 155
4 ppolymorphe 135
0 OriginalGriff 7,036
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,286
3 George Jonsson 2,840
4 Suvendu Shekhar Giri 2,216

Advertise | Privacy | Mobile
Web01 | 2.8.151120.1 | Last Updated 1 Sep 2011
Copyright © CodeProject, 1999-2015
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