2013-08-22 23 views
1

是否可以根據條件更改列值或單元值?
如何根據條件更改DataGridView列值

考慮我有一個DataGridView(即)3列,找到最大的兩個數

DataGridView輸入是從SQL Server得到。

第一個Datagridview列是A,第二個是B,第三列是查找A是否大於B。如果條件滿足,則應在第3列中顯示文本"TRUE""FALSE"

回答

0

使用服務器端方法,可以根據自己的條件更改單元格的值:

<asp:TemplateField HeaderText="Application No"> 
<ItemTemplate> 
<asp:Label ID="lbl" Text='<%# chkValue(Eval("A"),Eval("B")) %>' runat="server" /> 
    </ItemTemplate> 
    </asp:TemplateField> 

chkValue功能將兩個參數,並檢查其值越大,並返回相應的true/false。

+0

+1快速回復任何方式,我需要它的Windows窗體。我可以使用這個爲我的未來目的.. – coolprarun

0

試試這個

<asp:GridView runat="server" ID="gv"> 
     <Columns> 
      <asp:TemplateField HeaderText="a"> 
       <ItemTemplate> 
        <%# Eval("a") %> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="b"> 
       <ItemTemplate> 
        <%# Eval("b") %> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="display"> 
       <ItemTemplate> 
        <%# Convert.ToInt16(Eval("a"))>Convert.ToInt16(Eval("b"))?"True":"False" %> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 
+0

+1的快速回復任何方式我需要它的Windows窗體。我可以將其用於我未來的目的.. – coolprarun

0
Protected Sub dgrd_WWWH_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles dgrd_WWWH.RowDataBound 
If e.Row.RowType And DataControlRowType.DataRow Then 
       Dim ColA As Label = CType(e.Row.FindControl("colA"), Label) 
Dim ColB As Label = CType(e.Row.FindControl("colB"), Label) 
If Val(ColA) > Val(ColB) then 
dgrd_WWWH.Rows(e.Row.RowIndex).Cells(2).Text = "True" 
Else 
dgrd_WWWH.Rows(e.Row.RowIndex).Cells(2).Text = "False" 
End If 
End If 
End Sub 
1

我已經回答了這裏過類似的問題Changing Cell Values of DataGridView

可以使用DataGridView.CellFormatting事件。在你的情況下,當需要格式化第三個單元格時,需要爲每行檢索其他單元格的值。

示例代碼波紋管假設:

  • intDataGridView:你需要做適當的轉換。
  • NullDbNull值:如果需要,您需要檢查空值。

​​