Peter
Peter

Reputation: 351

Binding label inside a gridview from codebehind.

I want to bind my totalcomplete variable to lblComplete that is in my gridProgress but i don't know how to do it, can anybody help me out?

ASP Code:

<asp:GridView ID="gridProgress" runat="server" AutoGenerateColumns="False">
    <Columns>
       <asp:TemplateField HeaderText="Complete">
          <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" />
          <HeaderStyle HorizontalAlign="Center" />
          <ItemTemplate>
              <asp:Label ID="lblComplete" runat="server"></asp:Label>
          </ItemTemplate>
      </asp:TemplateField>
    </Columns>
</asp:GridView>

C# Code:

da = new SqlDataAdapter(sql, oConn);
da.Fill(ds);


//count number of tasks completed, not completed
int count = ds.Tables[0].Rows.Count;
string value = ds.Tables[0].Rows[0]["Completed"].ToString();
int completed = 0;
for (int i = 0; i < count; i++)
{
   if (value == "No")
   {
       completed++;
   }                
   int totalcomplete = completed;
   //here i want to bind totalcomplete to my lblCompleted
}

Upvotes: 1

Views: 2805

Answers (3)

शेखर
शेखर

Reputation: 17614

Your question is not clear.
Do you want to display the totlalComplete in each row
Or somewhere else?
As you have a template field inside gridview
So it is going to render in every row

And your loop too is strange
It should be as follow

int count = ds.Tables[0].Rows.Count;
int completed = 0;
for (int i = 0; i < count; i++)
{
   string value = ds.Tables[0].Rows[i]["Completed"].ToString();
   if (value == "No")
   {
      completed++;
   }                
   int totalcomplete = completed;
   //here i want to bind totalcomplete to my lblCompleted
}

Edit 1

Here is a link which may help you
Displaying Total in Footer of GridView and also Add Sum of columns(row vise) in last Column
http://csharpdotnetfreak.blogspot.com/2009/07/display-total-in-gridview-footer.html

Upvotes: 1

Praveen Nambiar
Praveen Nambiar

Reputation: 4892

Use the FindControl property of Gridview.

Label lbl = gv.Rows[rowIndex].FindControl("lblComplete") as Label;
lbl.Text = Convert.ToString(totalcomplete); 

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

You can use DataBinder.Eval method

Text='<%# DataBinder.Eval(Container.DataItem,"totalcomplete") %>'

Upvotes: 0

Related Questions