简体   繁体   English

如何根据条件更改DataGridView列值

[英]How to change DataGridView column value based on condition

Is it possible to change the column value or cell value based on the Condition? 是否可以根据条件更改列值或单元格值?

Consider I am having 3 columns in a DataGridView (ie) to find the greatest of two number 考虑我在DataGridView有3列(即)来找到两个中的最大值

The input from DataGridView is got from SQL Server. DataGridView的输入来自SQL Server。

First Datagridview column is A and second one is B and 3rd column is to find whether A is bigger than B or not. 第一个Datagridview列为A,第二个为B,第三列为查找A是否大于B。 if the condition satisfies it should display the text "TRUE" or else "FALSE" in 3rd Column. 如果条件满足,则应在第三列中显示文本"TRUE""FALSE"

I have answered a similar question here Changing Cell Values of DataGridView 我在这里回答了类似的问题更改DataGridView的单元格值

You can use the DataGridView.CellFormatting event. 您可以使用DataGridView.CellFormatting事件。 In your case you need to retrieve, for each Rows, the value of others Cells when the third Cell needs to be formatted. 在您的情况下,当需要格式化第三个单元格时,您需要为每个行检索其他单元格的值。

The sample code bellow supposes: 下面的示例代码假定:

  • values are int in DataGridView : you need to do the appropriate conversion. 值在DataGridViewint :您需要进行适当的转换。
  • no Null , DbNull Values: you need to check for null values if required. NullDbNull值:如果需要,您需要检查空值。

void dgv_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 2) {
        if (Convert.ToInt32(this.dgv.Rows[e.RowIndex].Cells[0].Value) > Convert.ToInt32(this.dgv.Rows[e.RowIndex].Cells[1].Value)) {
            e.Value = "TRUE";
        } else {
            e.Value = "FALSE";
        }
        e.FormattingApplied = true;
    }
}

Use Server Side method to change the value of cell according to your condition: 使用服务器端方法根据您的条件更改单元格的值:

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

chkValue function will take two arguments and check which value is greater and return true/false accordingly. chkValue函数将使用两个参数,并检查哪个值更大并相应返回true / false。

Try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM