Pit76
asked on
add values of datatable to DataGridView c#
hi all,
I have a datatable wich contains +20 columns. I need all the data from these columns but I only want to display 5 of them in my DataGridView.
So I've created 5 columns in the DGV and try to add the values of the specified columns in the datatable to it.
Problem 1: I only get the column name in the dgvcolumns instead of the value.
Problem 2: when I doubleClick a Row in the DGV I want to get all the +20 columns from the datatable in local vars.
Below some code how I try to fill my DGV with the 5 columns from the DT.
And how can I solve problem nr2?
Grts.
I have a datatable wich contains +20 columns. I need all the data from these columns but I only want to display 5 of them in my DataGridView.
So I've created 5 columns in the DGV and try to add the values of the specified columns in the datatable to it.
Problem 1: I only get the column name in the dgvcolumns instead of the value.
Problem 2: when I doubleClick a Row in the DGV I want to get all the +20 columns from the datatable in local vars.
Below some code how I try to fill my DGV with the 5 columns from the DT.
And how can I solve problem nr2?
Grts.
DataSet DsDocs = _LocalDocBLL.GetDocuments(1);
if (DsDocs.Tables[0].Rows.Count > 0)
{
// fill dataGridView
//dataGridViewOffertes.DataSource = DsDocs.Tables[0];
DataTable myDT = DsDocs.Tables[0];
for (int i = 0; i < myDT.Rows.Count; i++)
{
dataGridViewOffertes.Rows.Add();
dataGridViewOffertes.Rows[i].Cells["id"].Value = myDT.Columns["doc_id"];
dataGridViewOffertes.Rows[i].Cells["bedrag"].Value = myDT.Columns["doc_eindBedragDocument"];
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
I found the solution to my first prob to.
I needed to map the column value from my dataset to the DGV like this instead of what I tried earlier.
Grts and thx for your help on a saturday :)
I found the solution to my first prob to.
I needed to map the column value from my dataset to the DGV like this instead of what I tried earlier.
Grts and thx for your help on a saturday :)
dataGridViewOffertes.Rows[i].Cells["id"].Value = DsDocs.Tables[0].Rows[i]["doc_id"] ;
ASKER
I found the answers myself but thx for your time. :)
hi,
nice to know that you was able to resolve....
I was not online when you posted comments otherwise I could have posted response as the issue was not very critical
-thanks
:)
nice to know that you was able to resolve....
I was not online when you posted comments otherwise I could have posted response as the issue was not very critical
-thanks
:)
ASKER
I was able to solve problem nr 2 on my own. I just take the value of the ID column of the doubleClicked row and then perform a Select on my DT to search for the row with the matiching ID.
Then I fill a structure with the values of the row found.
About problem nr1: tried your solution on a number of columns and that works. But I only want 5 columns of the 40 in my dataset to show up in the DGV. So this would be lot's of work to do it like this.
Can you see what I'm doing wrong in my original solution. I want to loop throught the DS and add for each row find the values of the 5 columns to the DGV. It works but I don't get the value, only the rowheadertext shows up in all rows.
Grts!
Open in new window