Click here to Skip to main content
13,054,615 members (61,200 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 2:53am
Updated 1-Sep-11 3: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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 1 Sep 2011
Copyright © CodeProject, 1999-2017
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