Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Bind my Data to GridView (might be tricky)

Posted on 2007-11-20
20
Medium Priority
?
509 Views
Last Modified: 2012-06-27
My Data looks like this
(Return DataSet)
Attribute - AttributeValue - AttributeDate
FirstName          Joe                    10/10/2007
Last Name         Doe                   10/10/2007

The attributes are rows, but in my app i need to make these Columns in my Gridview.
Gridview should look like this:
FirstName    LastName      
Joe                  Doe

How can i bind the data set to my gridview when dataset returns my rows (when i need this as columns)?
0
Comment
Question by:Padre_Corleone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 6
  • 2
20 Comments
 
LVL 4

Expert Comment

by:kirankumar_goura
ID: 20326193
Here ds refers to DataSet.

foreach(DataColumn dc in ds.Tables[0].Columns)
{
         dataGridView1.Columns.Add(dc.ColumnName, dc.ColumnName);
}
foreach (DataRow dr in ds.Tables[0].Rows)
{
         dataGridView1.Rows.Add(dr.ItemArray);
}
dataGridView1.Show();

Let me know for further assistance.

- Kiran G
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 20326212
i'll try that right now...
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 20326234
I get the following errors:

value of type 'String' cannot be converted to 'System.Web.UI.WebControls.DataControlField'.
too many arguments to 'Public Sub Add(field As System.Web.UI.WebControls.DataControlField)'.
'Add' is not a member of 'System.Web.UI.WebControls.GridViewRowCollection'.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:kirankumar_goura
ID: 20326299
Apologies Dude, the above code works for windows application. The following code you need to use
to bind DataSet to a Grid ...

GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();

Let me know for further assistance.
- Kiran G
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 20326319
This binds the dataset but if you look at my first question string, i will need to flip my rows into columns :)
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20326384
You can try one trick

As you have Dataset with you ...right?

You can create one new datatable
Lik

dim dt as Datatable

Then just loop the dataset , and add the first row as column in the datatable like

dt.columns.Add( value here)

and for second column add one record in datatable



0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 20326408
can you show me an example? to bind to my gridview?

dim ds as dataset = mydataset information
dim dt as datatable = ds

               For Each dt In ds.Tables(0).Columns
                dt.Columns.Add(dt.Columns(0).ColumnName)
            Next
   
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20326578
dim ds as dataset = mydataset information
dim dt as datatable
Dim dr as DataRow

               For Each dr In ds.Tables(0).Rows

                dt.Columns.Add(dr.Item(0))
            Next
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 20326579
ok will try right now.
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 20326589
now, in your example what if i wand to add all rows to gridview column headers
and all columns to rows?
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 20326607
example:

 For Each dr In ds.Tables(0).Rows
            If dt.Columns.Add(dr.Item(0)) = "AttributeName" Then
             MyGridView.add.columns(dt.rows.item(0))
            End IF

???
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20326634
dim ds as dataset = mydataset information
dim dt as datatable
Dim dr as DataRow

               For Each dr In ds.Tables(0).Rows

                dt.Columns.Add(dr.Item(0))
            Next

after this


GridView1.DataSource = dt;
GridView1.DataBind();

0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 20326658
it says Dt already has a item of the same name, should i use a different (new datable)?
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20326666
you can try
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 20326707
cool thanks, my only problem now is since it is the same dt, i get the original table in gridview plus,
my rows as columns (as i wanted)
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 2000 total points
ID: 20326728
You can create new Datatable And add only rows as columns  , rather that using same
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 20326792
actually i get a object reference error whe i use a new datable
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20326804
no need to use new keyword with datatable
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 20326808
       Dim dt As DataTable = ds.Tables(0)
        Dim dr As DataRow

        For Each dr In ds.Tables(0).Rows
            If ds.Tables(0).Columns(1).ColumnName = "AttributeName" Then
                dt.Columns.Add(dr.Item(1))
            End If
        Next
        gridview.DataSource = dt
        gridview.DataBind()
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 20326812
I did not use new keyboard,
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

610 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