2013-08-22 20 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值:如果需要,您需要检查空值。

​​