Urgent please

S

settyv

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
 
S

seigo

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

"""(e-mail address removed) ÐÉÓÁÌ(Á):
"""
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top