2011-09-05 96 views
0

我將數據綁定到網格視圖中,我想在表中添加列狀態,並且列中的值將被髮送消息或消息保存。我想顯示的圖像基於值是消息發送或保存。我怎樣才能動態地做到這一點。根據asp.net中的列值在網格視圖中動態顯示圖像

 DataTable dt = new DataTable(); 

     try 
     { 
      string MysqlStatement = "SELECT MsgID, MsgText, RespondBy, ExpiresBy, OwnerName FROM tbl_message WHERE tbl_user_tbl_organisation_OrganisationID = @Value1"; 
      MySqlCommand sqlCmd = new MySqlCommand(MysqlStatement, connectionString); 
      sqlCmd.Parameters.AddWithValue("@Value1", newOrgID); 
      MySqlDataAdapter sqlDa = new MySqlDataAdapter(sqlCmd); 
      sqlDa.Fill(dt); 
      if (dt.Rows.Count > 0) 
      { 
       Grid_Messagetable.DataSource = dt; 
       Grid_Messagetable.DataBind(); 
       Grid_Messagetable.Columns[2].Visible = false; 
      } 
      else 
      { 
      } 


<asp:GridView ID="Grid_Messagetable" runat="server" AllowPaging="True" SelectedIndex="0" 
       DataKeyNames="MsgID" ShowHeaderWhenEmpty="True" OnRowDeleting="MsgTable_RowDeleting" 
       OnRowEditing="MsgTable_RowEditing" AutoGenerateColumns="False" BorderStyle="Double" 
       Width="537px"> 
       <Columns> 
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="Edit controls" 
         ButtonType="Image" DeleteImageUrl="~/Styles/Images/Delete.gif" EditImageUrl="~/Styles/Images/Edit.gif" /> 
        <asp:TemplateField HeaderText="DashBoard"> 
         <ItemTemplate> 

         <asp:ImageButton ID="imgbtn_ViewDashBoard" ImageUrl="Styles/Images/dash.png" Enabled="True" 
            Width="50" runat="server" PostBackUrl='<%# Eval("MsgID", "ResponseMetric.aspx?MsgID={0}") %>' 
            Text='Send'></asp:ImageButton> 

         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:BoundField DataField="MsgID" HeaderText="MsgID" ReadOnly="True" SortExpression="MsgID" /> 
        <asp:BoundField DataField="MsgText" HeaderText="MsgText" ReadOnly="True" SortExpression="MsgText" /> 
        <asp:BoundField DataField="RespondBy" HeaderText="RespondBy" ReadOnly="True" SortExpression="RespondBy" /> 
        <asp:BoundField DataField="ExpiresBy" HeaderText="ExpiresBy" ReadOnly="True" SortExpression="ExpiresBy" /> 
        <asp:BoundField DataField="OwnerName" HeaderText="OwnerName" ReadOnly="True" SortExpression="OwnerName" /> 
        <asp:ImageField DataImageUrlField="Sent" HeaderText="Status" 
         NullImageUrl="~/Styles/Images/White.gif"> 
        </asp:ImageField> 
       </Columns> 
      </asp:GridView> 

回答

0

您可以創建模板列字段是這樣的:

<asp:TemplateField HeaderText="Status"> 
    <ItemTemplate> 
      <asp:ImageButton ID="img" runat="server" ImageUrl='<%# (Eval("Status") == "Sent") ? "messagesent.png" : "messagesaved.png" %>' /> 
    </ItemTemplate> 
</asp:TemplateField>