Bind my Data to GridView (might be tricky)

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)?
LVL 7
Padre_CorleoneAsked:
Who is Participating?
 
Pratima PharandeConnect With a Mentor Commented:
You can create new Datatable And add only rows as columns  , rather that using same
0
 
kirankumar_gouraCommented:
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
 
Padre_CorleoneAuthor Commented:
i'll try that right now...
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Padre_CorleoneAuthor Commented:
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
 
kirankumar_gouraCommented:
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
 
Padre_CorleoneAuthor Commented:
This binds the dataset but if you look at my first question string, i will need to flip my rows into columns :)
0
 
Pratima PharandeCommented:
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
 
Padre_CorleoneAuthor Commented:
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
 
Pratima PharandeCommented:
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
 
Padre_CorleoneAuthor Commented:
ok will try right now.
0
 
Padre_CorleoneAuthor Commented:
now, in your example what if i wand to add all rows to gridview column headers
and all columns to rows?
0
 
Padre_CorleoneAuthor Commented:
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
 
Pratima PharandeCommented:
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
 
Padre_CorleoneAuthor Commented:
it says Dt already has a item of the same name, should i use a different (new datable)?
0
 
Pratima PharandeCommented:
you can try
0
 
Padre_CorleoneAuthor Commented:
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
 
Padre_CorleoneAuthor Commented:
actually i get a object reference error whe i use a new datable
0
 
Pratima PharandeCommented:
no need to use new keyword with datatable
0
 
Padre_CorleoneAuthor Commented:
       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
 
Padre_CorleoneAuthor Commented:
I did not use new keyboard,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.