Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have table in table tb1 like(table only one clounm)
ID
100
200
300
100
200
200
i used this query
select ID, count(ID) as pair From tb1 group by MemberID HAVING Count(ID)>0
i got result
id pair
100 2
200 3
300 1
i need result like this in gridview

ID pair
100 2
200 3
300 1

i write this code in rowdatbound
C#
protected void gridtree_RowDataBound(object sender, GridViewRowEventArgs e)
   {
if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblmessage = (Label)e.Row.FindControl("lblmessage");

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lblmessage.Text = dt.Rows[i]["pair"].ToString();
                  

                }
            }
        }
   }


I am getting this result
ID pair
100 1
200 1
300 1

I need result

Id pair
100 2
200 3
300 1
Posted
Updated 9-Apr-12 4:28am
v2

As seen from the following code given in the question,
C#
for (int i = 0; i < dt.Rows.Count; i++)
{
    lblmessage.Text = dt.Rows[i]["pair"].ToString();
}

each time the gridtree_RowDataBound event is fired, all rows of the DataTable dt are traversed in the for loop and the lblmessage.Text is set, which is being over written in each iteration. So the last value in the last row of the DataTable dt, which is 1 as seen from the data given in the question, will be visible in the lblmessage after the above event.
But I think, for the purpose of displaying the data in GridView a DataTable with columns Id, pair can be created and populated as per requirement. Then it can be used to bind to the gridview in a normal way. You can consider this alternative.
 
Share this answer
 
try this query

Select ID , (select count(tabel1.ID) from tabel1
where tabel1.ID = tabel.ID
HAVING Count(tabel1.ID)>0 )
from tabel


and u dont need to check for if (dt.Rows.Count > 0)
 
Share this answer
 
Comments
rajnish singh rathor 10-Apr-12 2:19am    
thanks..but i got another way
just take gidview like this only


XML
<asp:GridView ID="gridtree1" runat="server" AutoGenerateColumns="false"
                          Width="100%" height="50%"
               OnRowDataBound="gridtree_RowDataBound" style="margin-top: 3px" >

                            <Columns>
                            <asp:BoundField ReadOnly="true" HeaderText="ID" DataField="MemberID" SortExpression="MemberID" />
                            <asp:BoundField ReadOnly="true" HeaderText="pair" DataField="pair" SortExpression="pair" />
<%--                            <asp:BoundField ReadOnly="true" HeaderText="award" DataField="pair" SortExpression="pair" />--%>
                             <asp:TemplateField HeaderText="Award" SortExpression="Pair">
                                 <ItemTemplate>
                                  <asp:Label ID="lblmessage" runat="server"> </asp:Label>
                                </ItemTemplate>
                               </asp:TemplateField>

                             </Columns>
                              </asp:GridView>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900