2016-08-07 6 views
1

私はitemtemplateを含むgridviewを持っていて、itemtemplatelabelを含んでいます。 textlabelに置きたいのですが、値はcodebehindです。 outputgridviewに入力します。細かい作業 IMGラベルを使用してグリッドビューの行に値を動的に渡す方法は?

Proj_TitleTotal_Issueが、すべての行に対してlabelテキストを設定する方法について説明します。ここ は私のaspxコードです:

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

count値が2その後、結果であるとき、私が欲しい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"; 
} 
} 
} 

Good行に対してResult列に示されており、count値が5あるときに発生します​​となり、ラベルはResult列になります。お気軽に

+0

あなたはpageloadにGridViewのデータテーブルにバインドしないのはなぜ? – Developer

+0

私はページload.Youで 'gridview'の値を​​取得したいのですが –

+0

を教えてください。しかし、あなたは何が欲しいですか?グリッドビューでのデータのバインドについて混乱していると思います。 – Developer

答えて

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

+0

あなたの要件に応じてif条件を変更できます.... –

関連する問題