Hi friend,
at form load its' requiremewnt that i should display the Structure of the DataGridView columns as:
I ahve designed teh following datatable:
DataTable dtProposedMaterials = new DataTable("dtPropMaterials");
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");
|
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
petre ricardoPosted Mar 15, 2010, 9:48 PM
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
Sam HobbsPosted Mar 15, 2010, 4:11 PM
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 ricardoPosted Mar 15, 2010, 3:58 AM
appologies for removing my original thread becuase i thought they were misleading :( so i thought to post it in new with more descriptively
Amit ChoudharyPosted Mar 15, 2010, 3:04 AM
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.
Sam HobbsPosted Mar 15, 2010, 2:38 AM
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.
petre ricardoPosted Mar 15, 2010, 1:28 AM
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
Amit ChoudharyPosted Mar 15, 2010, 1:09 AM
Remove the column from your datatable or data set before bind it to Data Grid View
Please mark as answer if it helps you.