Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Managing data in DataGridView column
 

Managing data in DataGridView column

I have a data table with few columns, and I have added one morecolumn in appropriate DataGridView, with Designer. How can I add data in that column - it is a string type?
Nebojša Mančić  Tuesday, August 19, 2008 10:51 PM

Well, that depends on different conditions, such as: whether has DataGridView set its DataSource? What kind of Column is populated? Bound or unbound? We can only provide accurate enough solutionafter getting moreinformation about them.

However, I'll try me best to answer:

1. If you have added a column with any type other than ComboBoxColumn, and have set DataGridView.DataSource, just set new column’s “DataPropertyName?will make it displaying data. It’s under bound manner of course.

2. If you have added a ComboBoxColumn, and want to make it as bound ComboBoxColumn, set its “DataSource? “DisplayMember?will do.

3. And if you want to use it as unbound and want to add values to it, you can add value in some event, for example, button’s click event. Then you can manipulate this auto populated value, whether edit them or save them.

Code Snippet

private void button1_Click(object sender, EventArgs e)

for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)

"Default Value";

For your information, here is an article about manipulating DataGridView Columns, believe it will help:

How to: Manipulate Columns in the Windows Forms DataGridView Control

Please do not hesitate to let me know if there is any question.

Best wishes,

Jun Wang

Jun Wang Tim  Friday, August 22, 2008 6:42 AM

Yes, but there is problem with "Default Value", I have got this error

Error2Cannot implicitly convert type 'string' to 'System.Windows.Forms.DataGridViewCell'D:\Documents and Settings\neman\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs4369WindowsFormsApplication1

And I have found the solution for this problem, something like this

private void toolStripButton1_Click(object sender, EventArgs e)

{

for (int i = 0; i <= tabela1DataGridView.Rows.Count - 2; i++)

{

DataGridViewCell cell =tabela1DataGridView.RowsIdea.Cells[2] ;

cell.Value = "MANCA "+i.ToString();

}

}

Thanks Jun!

Nebojša Mančić  Friday, August 22, 2008 10:05 AM

Hi Nebojša

Well, two possibilities:

1. If the new column is in the same DataGridView as other columns, click “Edit Columns?in DataGridView’s Designer, then set new column’s “DataPropertyName?to the field of table that you want to display. Then it’ll be OK.

2. If the new column is in a different DataGridView, set DataGridView.DataSource first, then to the thing as listed in 1st step.

Please do not hesitate to let me know if there is any question.

Thanks.

Best wishes

Jun Wang

Jun Wang Tim  Thursday, August 21, 2008 7:07 AM

Thanks, but my problem is how can I assign a value to the addedd column. I can see the column, but without values ?
Nebojša Mančić  Thursday, August 21, 2008 3:05 PM

Well, that depends on different conditions, such as: whether has DataGridView set its DataSource? What kind of Column is populated? Bound or unbound? We can only provide accurate enough solutionafter getting moreinformation about them.

However, I'll try me best to answer:

1. If you have added a column with any type other than ComboBoxColumn, and have set DataGridView.DataSource, just set new column’s “DataPropertyName?will make it displaying data. It’s under bound manner of course.

2. If you have added a ComboBoxColumn, and want to make it as bound ComboBoxColumn, set its “DataSource? “DisplayMember?will do.

3. And if you want to use it as unbound and want to add values to it, you can add value in some event, for example, button’s click event. Then you can manipulate this auto populated value, whether edit them or save them.

Code Snippet

private void button1_Click(object sender, EventArgs e)

for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)

"Default Value";

For your information, here is an article about manipulating DataGridView Columns, believe it will help:

How to: Manipulate Columns in the Windows Forms DataGridView Control

Please do not hesitate to let me know if there is any question.

Best wishes,

Jun Wang

Jun Wang Tim  Friday, August 22, 2008 6:42 AM

Yes, but there is problem with "Default Value", I have got this error

Error2Cannot implicitly convert type 'string' to 'System.Windows.Forms.DataGridViewCell'D:\Documents and Settings\neman\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs4369WindowsFormsApplication1

And I have found the solution for this problem, something like this

private void toolStripButton1_Click(object sender, EventArgs e)

{

for (int i = 0; i <= tabela1DataGridView.Rows.Count - 2; i++)

{

DataGridViewCell cell =tabela1DataGridView.RowsIdea.Cells[2] ;

cell.Value = "MANCA "+i.ToString();

}

}

Thanks Jun!

Nebojša Mančić  Friday, August 22, 2008 10:05 AM

You can use google to search for other answers

Custom Search

More Threads

• Make a DataGrid show realtime data when underlying values change???
• Canceling DataGridView Selection Change
• Error: Sort string contains a property that is not in the IBindingList.
• CallBack in GridView
• Loop through winform datagridview and count checkboxes that are checked
• CTRL+C problem in Datagrid forms control
• CREATING AND USING DATAVIEW OBJECTS
• Custom methods and the repeater control
• Change of DataSource of BindingSource does not reflects in DataGridView
• Deleted row information cannot be accessed through the row.