By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,993 Members | 1,898 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,993 IT Pros & Developers. It's quick & easy.

Urgent please

P: n/a
Hi,

I need to merge the rows in the datagrid based on the condition.Please
let me know how can i do that.Here is the scenario:

Present View:

Short name Filed by
A P
A Q
A R
B S
B T
B U
C V
C W

But i need to display like this:
Short Name Filed By
A P,Q,R
B S,T,U
C V,W

For that i used itemtemplate in the grid control as below:

<asp:datagrid id="grdRegister" Runat="server"
OnPageIndexChanged="NewPage" Headerstyle-BackColor="#9B9BB4"
Width="100%" HeaderStyle-Font-Bold="True"
HeaderStyle-ForeColor="#000000" HeaderStyle-CssClass="tableheader"
AutoGenerateColumns="False"
Font-Name="verdana">
<AlternatingItemStyle BackColor="#e2e2e2" Font-Name="verdana"
/>
<Columns>
<asp:BoundColumn DataField="Short/Long Entry"
HeaderText="Short/Long Entry" />

<asp:TemplateColumn HeaderText="Filed by">
<ItemTemplate >
<asp:Label runat="server" Text='<%# string.Format( "{0},
{1}",
DataBinder.Eval(Container, "DataItem.Filed By"),
DataBinder.Eval(Container, "DataItem.Short/Long Entry") ) %>'
ID="Label4">
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
Please let me know how can i achieve that.

Thanks,
Vishnu

Nov 10 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi,

You need to create a new DataTable and use it as data source for
GridView:

DataTable table = new DataTable("table");
DataColumn colShortName = new DataColumn("SortName",
Type.GetType("System.String"));
table.Columns.Add(colShortName);
DataColumn colFiledBy = new DataColumn("FiledBy",
Type.GetType("System.String"));
table.Columns.Add(colFiledBy);

// here you need to inerate through all rows in your dataset or
whatever you get from the database

foreach (DataRow row in ...) {
DataRow[] rows = table.Select("ShortName = '" + row["ShortName"] +
"'");
if (rows.Count 0) {
DataRow r = rows[0];
r["FiledBy"] += ", " + row["FiledBy"];
} else {
DataRow newRow = table.NewRow();
newRow["ShortName"] = row["ShortName"];
newRow["FiledBy"] = row["FiledBy"];
table.Rows.Add(newRow);
}
}

grdRegister.DataSource = table;
grdRegister.DataBind();

Alexander Kleshchevnikov
www.klalex.com

"""se****@gmail.com ΠΙΣΑΜ(Α):
"""
Hi,

I need to merge the rows in the datagrid based on the condition.Please
let me know how can i do that.Here is the scenario:

Present View:

Short name Filed by
A P
A Q
A R
B S
B T
B U
C V
C W

But i need to display like this:
Short Name Filed By
A P,Q,R
B S,T,U
C V,W

For that i used itemtemplate in the grid control as below:

<asp:datagrid id="grdRegister" Runat="server"
OnPageIndexChanged="NewPage" Headerstyle-BackColor="#9B9BB4"
Width="100%" HeaderStyle-Font-Bold="True"
HeaderStyle-ForeColor="#000000" HeaderStyle-CssClass="tableheader"
AutoGenerateColumns="False"
Font-Name="verdana">
<AlternatingItemStyle BackColor="#e2e2e2" Font-Name="verdana"
/>
<Columns>
<asp:BoundColumn DataField="Short/Long Entry"
HeaderText="Short/Long Entry" />

<asp:TemplateColumn HeaderText="Filed by">
<ItemTemplate >
<asp:Label runat="server" Text='<%# string.Format( "{0},
{1}",
DataBinder.Eval(Container, "DataItem.Filed By"),
DataBinder.Eval(Container, "DataItem.Short/Long Entry") ) %>'
ID="Label4">
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
Please let me know how can i achieve that.

Thanks,
Vishnu
Nov 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.