Click here to Skip to main content
15,888,337 members
Please Sign up or sign in to vote.
5.00/5 (1 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:


C#
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
Updated 1-Sep-11 3:15am
v3

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.
 
Share this answer
 
You can insert a column into your table manually, and then set the value of each row like this:

C#
table.Columns.Add("Cost", typeof(decimal));
            foreach (DataRow row in table.Rows)
            {
                row["Cost"] = 100;
            }



Hope this helps
 
Share this answer
 
Comments
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;
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 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)



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