如何使用label将值动态传递给gridview行?

时间:2016-08-07 14:22:32

标签: c# asp.net gridview

gridview包含itemtemplateitemtemplate内包含label。我想将text放在label中,哪些值来自codebehind。 我想在output中使用gridview这种类型的Proj_TitleIMG

Total_Issuelabel工作正常,但如何针对每一行设置 <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代码:

 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";
 }
 } 
 } 

ASPX.CS

count

我希望当2值为Good时,结果将Result列显示在count列中,而5值为Poor然后结果将是Result并设置标签const emailFragment = Relay.QL` fragment on Email { from to body subject } `; export default Relay.createContainer(Component, { initialVariables: { emailId: $emailId }, fragments: { interview: () => Relay.QL` fragment on Interview { recordId email { ${emailFragment} } } `, viewer: () => Relay.QL` fragment on User { recordId email(id: $emailId) { ${emailFragment} } } ` } }); 列。谢谢

1 个答案:

答案 0 :(得分:0)

您可以在网格上调用函数,从代码隐藏到设计页面。

<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页面

    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;
    }

输出结果
enter image description here