简体   繁体   中英

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

The input from DataGridView is got from 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. if the condition satisfies it should display the text "TRUE" or else "FALSE" in 3rd Column.

I have answered a similar question here Changing Cell Values of DataGridView

You can use the DataGridView.CellFormatting event. 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.
  • no Null , DbNull Values: you need to check for null values if required.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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