Solved

add values of datatable to DataGridView c#

Posted on 2009-05-09
5
1,891 Views
Last Modified: 2012-05-06
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.


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"];

          }

        }

Open in new window

0
Comment
Question by:Pit76
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
needo_jee earned 500 total points
ID: 24345218
hi,

there could be a lots of possibilities . here is one out of them.

Create dataset or datatable.
bind datatable to gridview but make the columns mapping hidden that you dont want to be displayed.



like this

 DsDocs.Tables[0].Columns[0].ColumnMapping = MappingType.Hidden;
 DsDocs.Tables[0].Columns[1].ColumnMapping = MappingType.Hidden;


 dataGridViewOffertes.datasource=dsDocs.tables[0];




Now to get values in doubleClick.... just use the currentRow.Index to get specific data row from table

like this
private void dataGridViewOffertes_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string myfirstColumnVAlue =DsDocs.Tables [0].Rows [e.RowIndex ]["< name of first column >"]
            string my2ndColumnVAlue =DsDocs.Tables [0].Rows [e.RowIndex ]["< name of 2nd column >"]
        }


I have made DsDocs a class member instead loacal, so that its available on whole form.

-Thanks



0
 

Author Comment

by:Pit76
ID: 24347382
Hi,  thx for your comment.

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!
// Solution prob nr2

// Get doc_id selected document

      DataGridViewRow dr = dataGridViewOffertes.SelectedRows[0];

      int doc_Id = Convert.ToInt32(dr.Cells["id"].Value);
 

      // find DataTableRow with the doc_id in myDT

      DataRow[] row = myDT.Select("[doc_id] = " + doc_Id);
 

      // fill docStruct with values from myDT

      #region Fill docStruct

      docStruct.id = Convert.ToInt32(row[0]["doc_id"]);

      docStruct.type_id = Convert.ToInt32(row[0]["doc_type_id"]);

      docStruct.btw_id = Convert.ToInt32(row[0]["doc_btw_id"]);
 
 
 

//prob1

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"];

          }

Open in new window

0
 

Author Comment

by:Pit76
ID: 24347465
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 :)




dataGridViewOffertes.Rows[i].Cells["id"].Value = DsDocs.Tables[0].Rows[i]["doc_id"] ;

Open in new window

0
 

Author Closing Comment

by:Pit76
ID: 31579759
I found the answers myself but thx for your time. :)
0
 
LVL 12

Expert Comment

by:needo_jee
ID: 24351078
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
:)


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 48
bulid json format 3 46
XAML: Layout 8 25
Help with C#, MVC, razor. 6 23
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now