anees khan anees khan - 3 months ago 11
ASP.NET (C#) Question

how to pass values dynamically to gridview rows using label?

I've

gridview
which contain
itemtemplate
and inside
itemtemplate
contains
label
. I want to place
text
in
label
which values comes from
codebehind
.
I want this type of
output
in
gridview
.
IMG

Proj_Title
and
Total_Issue
working fine but how to set
label
text against every row .
Here is my aspx code :

<asp:GridView ID="g9" runat="server" OnRowDataBound="g9_RowDataBound" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Project_Title">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("proj_title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Issue">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Count") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RESULT">
<ItemTemplate>
<asp:Label ID="issue" runat="server" ForeColor="Blue" Font-Size="Large" Width="200px" ></asp:Label>
<asp:Label ID="issue2" runat="server" ForeColor="Blue" Font-Size="Large" Width="200px" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


ASPX.CS

protected void Page_Load(object sender, EventArgs e)
{
SqlCommand cmd3 = new SqlCommand("USE SKDM SELECT proj_title , COUNT (*) as Count FROM Issue WHERE proj_title is NOT null GROUP BY proj_title", cnn);
SqlDataAdapter adp = new SqlDataAdapter(cmd3);
DataSet ds = new DataSet();
adp.Fill(ds);
cnn.Open();
cmd3.ExecuteNonQuery();
cnn.Close();
g9.DataSource = ds;
g9.DataBind();
}
protected void g9_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SqlCommand cmd3 = new SqlCommand("USE SKDM SELECT proj_title , COUNT (*) as Count FROM Issue WHERE proj_title is NOT null GROUP BY proj_title", cnn);
cnn.Open();
SqlDataReader dr = cmd3.ExecuteReader();
int[] arr = new int[2];
while (dr.Read())
{
arr[0] = dr.GetInt32(1);
}
if (arr[0] == 2)
{
Label l3 = (Label)e.Row.FindControl("issue2");
string s = "BAD";
l3.Text = s;
}
dr.Close();
cnn.Close();
if (arr[0] == 2)
{
Label l2 = (Label)e.Row.FindControl("issue");
string s = "GOOD";
l2.Text = s;
}
if (arr[1] == 5)
{
Label ab = (Label)e.Row.FindControl("issue2");
ab.Text = "Poor";
}
}
}


I want when
count
value is
2
then result will be
Good
shown in
Result
column against row and when
count
value is
5
then result will be
Poor
and set label
Result
column .Thanks

Answer

Simply You Can call a function on your grid from code behind to Design page.

<asp:GridView ID="g9" runat="server" OnRowDataBound="g9_RowDataBound" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:TemplateField HeaderText="Project_Title">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("proj_title") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Total Issue">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("Count") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="RESULT">
                <ItemTemplate>
                     <%#ConvertResult(Convert.ToString(Eval("Count")))%>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

.CS Page

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                        new DataColumn("proj_title", typeof(string)),
                        new DataColumn("Count",typeof(int))});
        dt.Rows.Add(1, "SQl", 2);
        dt.Rows.Add(2, "C#", 5);
        g9.DataSource = dt;
        g9.DataBind();
    }

    protected void g9_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           // i dont use this...
        }
    }

    // Create this function and call this to in your gridview
    // see above in design page
    public string ConvertResult(string strCount)
    {
        string strResult = "";
        int iValue = Convert.ToInt32(strCount);
        if (iValue < 2)
            strResult = "Bad";
        if (iValue == 2)
            strResult = "Poor";
        if (iValue == 5)
            strResult = "great";
        return strResult;
    }

Output Result
enter image description here