Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gridview Layout 1

Status
Not open for further replies.

rsshetty

Programmer
Dec 16, 2003
236
US
I have a dataset of the following nature:

Col1 Col2 Col3
--------------
ClassA 10 12
ClassA 13 11
ClassA 18 22
ClassA 20 42
ClassB 12 34
ClassB 32 27
ClassB 13 51
ClassB 12 32
ClassC 16 65
ClassC 12 33


I want the gridview to have 3 separate HTML tables on the web page - one for rows of classA, the second for rows of classB and the thris for rows of classC.

Can this be done? I was hoping for some ideas.

Thanks



rsshetty.
It's always in the details.
 
You can create 3 separate datagrids and split your dataset into 3 data tables
 
filter your datatable into 3 data views. bind the views to the grids. example

Code:
DataTable table = routine.GetDataSet().Tables[0];
DataView viewA = new DataView(table, "[Col1] = 'ClassA'", "", DataViewState.CurrentRows);
DataView viewB = new DataView(table, "[Col1] = 'ClassB'", "", DataViewState.CurrentRows);
DataView viewC = new DataView(table, "[Col1] = 'ClassC'", "", DataViewState.CurrentRows);

myControlA.DataSource = viewA;
myControlA.DataBind();

myControlB.DataSource = viewB;
myControlB.DataBind();

myControlC.DataSource = viewC;
myControlC.DataBind();

if this data is for read only. I would use a repeater control with a templated HTML table instead of a gridview. example
Code:
<asp:repeater runat="server" id="myControlA">
   <HeaderTemplate>
      <table><thead><tr><th>Column 2</th>><th>Column 3</th></tr></thead><tbody>
   <HeaderTemplate>
   <ItemTemplate>
      <tr><td>
           <asp:Label runat="server" id="col2" Text='<#%Eval("Col2")#>'/>
          </td>
           <asp:Label runat="server" id="col3" Text='<#%Eval("Col3")#>'/>
          <td>
          </td></tr>
   </ItemTemplate>
   <FooterTemplate>
      </tbody>
      </table>
   </FooterTemplate>
</asp:repeater>

<!-- repeat for grids B and C-->

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I should have indicated that the number of groups is dynamic. What then?

rsshetty.
It's always in the details.
 
Actually I did think of the nested gridview. I am unsure how to implement it.
In the itemdatabound event of the outer gridview, I can bind the dataset to the inner gridview. However, how do I separate the inner gridviews based on Col1?

I am new to .NET, so please pardon my ignorance.

rsshetty.
It's always in the details.
 
here is an MS post on how to "group by" in a dataset. you'll want to use this for column on.

if the users are not editing the data I would use a repeater instead of a gridview. gridview are considered "heavy" especially if you just displaying read-only data.

ok, so after use the link provided you'll have a dataset like this
Code:
DataSet {
  Table[0] {
     Column1,
     Column2,
     Column3
  } 
  Table[1] {
     Column1
  }
  Relationship[0] {
     PK Table[1].Column1, 
     FK Table[0].Column1
 }
}
on setup your aspx
Code:
<asp:Repeater id="myRepeater" runat="server" OnItemDataBound="ItemDataBound">
  <ItemTemplate>
     <asp:Label id="myLabel" runat="server" Text='<#% Eval("Column1")#>' />
     <asp:Repeater id="nestedRepeater" runat="server">
       <HeaderTemplate>
           <table><thead>
              <tr>th>Column 2</th><th>Column 3</th></tr>
           </thead><tbody>
       </HeaderTemplate>
       <ItemTemplate>
          <tr><td>
           <asp:Label runat="server" id="col2" Text='<#%Eval("Col2")#>'/>
          </td>
          <td>
           <asp:Label runat="server" id="col3" Text='<#%Eval("Col3")#>'/>
          </td></tr>
       </ItemTemplate>
       <FooterTemplate>
          </tbody></table>
       </FooterTemplate>
     </asp:Repeater>
  <ItemTemplate>
</asp:Repeater>
now in your code behind
Code:
Page_Load...
{
   if(!page.IsPostback)
   {
      DataSet ds = getData(); //returns dataset
      //configure grouping...
      myRepeater.DataSource = ds;
      myRepeater.DataMember = ds.Tables[0].TableName;
      myRepeater.DataBind();
   }
}
protected void ItemDataBound(object sender, ItemDataBoundArgs e)
{
    if(e.Row.RowIndex > -1)
    {
       DataRowView view = (DataRowView)e.Row.DataItem;
       DataRelation relation = view.Row.Table.DataSet.Relations[0];
       Repeater ctrl = (Repeater)e.Row.FindControl("nestedRepeater");

       ctrl.DataSource = view.CreateChildView(relation);
       ctrl.DataBind();
    }
}
note: this is untested, but should point you in the right direction.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top