Click here to Skip to main content
12,066,626 members (56,466 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)
        {
            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 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


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