When i Assign the DataGridVIew control with the created table, it diplays like this:
> as you can it doesnt display the data in the proper columns instead it display the data in the to the right side by creating identical duplicate columns.
But i dont want it to dsiplay data like that, i want it to remove adding duplicate columns of the right side and add all the data to the orginal columns of the left side... HOw do i do this?
thanks
7 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 14, 2010, 9:09 PM EDT
0
hi friend,
Remove the column from your datatable or data set before bind it to Data Grid View
petre ricardo155 reputationPosted Mar 14, 2010, 9:28 PM EDT
0
hi friend, that will be same as commenting these lines: //dtProposedMaterials.Columns.Add("Code", typeof(string)); //dtProposedMaterials.Columns.Add("Material Type", typeof(string)); //dtProposedMaterials.Columns.Add("Material Name", typeof(string)); //dtProposedMaterials.Columns.Add("Amount Required", typeof(decimal));
Sam HobbsRank #107 · 20.5K reputationPosted Mar 14, 2010, 10:38 PM EDT
0
Please stop deleting threads when I try to help you and you don't like the answer. I am trying to explai how to do it the easy way so that this current problem will be easy to solve. The followig is the answer I provided in the thread (# 81313) that you deleted. If you do it the way I am suggesting, then solving the problem you describe in this thread is as easy as modifying the SQL. If you continue to delete threads when people try to help you, people will stop trying to help.
If you want someone to do all the code for you, then you can get the Visual Studio and C# to do it for you. Using the Data Sources window, create a data source for the database, then drag the table from the data source to the form and a DataGridView will be created for you.
If you need to do it the less easy way, you can do something such as the following. In this code, "Command" is a SQL Select command and ConnectionString is the connection string.
Code
SqlDataAdapter da = new SqlDataAdapter(Command, ConnectionString);
da.Fill(dtProposedMaterials);
BindingSource bs = new BindingSource();
bs.DataSource = dtProposedMaterials;
dataGridView1.DataSource = bs;
0
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 14, 2010, 11:04 PM EDT
0
hi peter, your following line are ok nothing wrong with these lines: dtProposedMaterials.Columns.Add("Code", typeof(string)); dtProposedMaterials.Columns.Add("Material Type", typeof(string)); dtProposedMaterials.Columns.Add("Material Name", typeof(string)); dtProposedMaterials.Columns.Add("Amount Required", typeof(decimal));
add following lines before binding the table to gridview : dtProposedMaterials.Column.RemoveAt(0); dtProposedMaterials.Column.RemoveAt(1); dtProposedMaterials.Column.RemoveAt(2); These column will be removed. Hope it helps.
petre ricardo155 reputationPosted Mar 14, 2010, 11:58 PM EDT
0
hi sam,
appologies for removing my original thread becuase i thought they were misleading :( so i thought to post it in new with more descriptively
0
Sam HobbsRank #107 · 20.5K reputationPosted Mar 15, 2010, 12:11 PM EDT
0
You are certainly correct that you have described the problem better. Note that even now, with the improved description of the problem, my suggestion remains the same. I hope I do understand the problem adequately.
It is frustrating for me that most of the developers in these forums are either not familiar with the conveniences that Visual Studio offers, or choose to ignore them. There is a lot that VS will do for us if we choose to become familiar with them. I think that part of the problem is that it is often easier to post code than it is to post the procedures to do things interactively, yet the interactive procedures can be easier for the developer using them. I agree that it is useful to know how to write the code so that it is easier to do more advanced things that the interactive procedures do not support, but I think it is also useful to know how to use the IDE to do what it can do.
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 14, 2010, 9:09 PM EDT
Remove the column from your datatable or data set before bind it to Data Grid View
Please mark as answer if it helps you.
petre ricardo155 reputationPosted Mar 14, 2010, 9:28 PM EDT
that will be same as commenting these lines:
//dtProposedMaterials.Columns.Add("Code", typeof(string));
//dtProposedMaterials.Columns.Add("Material Type", typeof(string));
//dtProposedMaterials.Columns.Add("Material Name", typeof(string));
//dtProposedMaterials.Columns.Add("Amount Required", typeof(decimal));
and leaving these lines:
dtProposedMaterials.Rows.Add("MKL001", "MTYP01", "Zinc", "10.23");
dtProposedMaterials.Rows.Add("MKL002", "MTXP04", "Copper", "0.89");
dtProposedMaterials.Rows.Add("MKL003", "MTCP05", "Aluminum", "1.23");
dtProposedMaterials.Rows.Add("MKL004", "MTTG07", "Magnesium", "11.23");
if i do that this is what i get:
thanks
Sam HobbsRank #107 · 20.5K reputationPosted Mar 14, 2010, 10:38 PM EDT
If you want someone to do all the code for you, then you can get the Visual Studio and C# to do it for you. Using the Data Sources window, create a data source for the database, then drag the table from the data source to the form and a DataGridView will be created for you.
If you need to do it the less easy way, you can do something such as the following. In this code, "Command" is a SQL Select command and ConnectionString is the connection string.
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 14, 2010, 11:04 PM EDT
your following line are ok nothing wrong with these lines:
dtProposedMaterials.Columns.Add("Code", typeof(string));
dtProposedMaterials.Columns.Add("Material Type", typeof(string));
dtProposedMaterials.Columns.Add("Material Name", typeof(string));
dtProposedMaterials.Columns.Add("Amount Required", typeof(decimal));
dtProposedMaterials.Rows.Add("MKL001", "MTYP01", "Zinc", "10.23");
dtProposedMaterials.Rows.Add("MKL002", "MTXP04", "Copper", "0.89");
dtProposedMaterials.Rows.Add("MKL003", "MTCP05", "Aluminum", "1.23");
dtProposedMaterials.Rows.Add("MKL004", "MTTG07", "Magnesium", "11.23");
add following lines before binding the table to gridview :
dtProposedMaterials.Column.RemoveAt(0);
dtProposedMaterials.Column.RemoveAt(1);
dtProposedMaterials.Column.RemoveAt(2);
These column will be removed.
Hope it helps.
petre ricardo155 reputationPosted Mar 14, 2010, 11:58 PM EDT
appologies for removing my original thread becuase i thought they were misleading :( so i thought to post it in new with more descriptively
Sam HobbsRank #107 · 20.5K reputationPosted Mar 15, 2010, 12:11 PM EDT
It is frustrating for me that most of the developers in these forums are either not familiar with the conveniences that Visual Studio offers, or choose to ignore them. There is a lot that VS will do for us if we choose to become familiar with them. I think that part of the problem is that it is often easier to post code than it is to post the procedures to do things interactively, yet the interactive procedures can be easier for the developer using them. I agree that it is useful to know how to write the code so that it is easier to do more advanced things that the interactive procedures do not support, but I think it is also useful to know how to use the IDE to do what it can do.
petre ricardo155 reputationPosted Mar 15, 2010, 5:48 PM EDT
i will start a new thread with some clear code :)
here is the link to it: http://www.c-sharpcorner.com/Forums/ShowMessages.aspx?ThreadID=81450
thanks