2

是否可以根据条件更改列值或单元格值?

考虑我在DataGridView(ie) 中有 3 列来找到两个数字中的最大值

输入来自DataGridViewSQL Server。

Datagridview 的第一列是 A,第二列是 B,第三列是查找 A 是否大于 B。如果条件满足,则应在第 3 列中 显示文本"TRUE"或其他内容。"FALSE"

4

4 回答 4

1

我已经回答了一个类似的问题在这里更改 DataGridView 的单元格值

您可以使用该DataGridView.CellFormatting事件。在您的情况下,当需要格式化第三个单元格时,您需要为每个行检索其他单元格的值。

下面的示例代码假设:

  • intDataGridView:您需要进行适当的转换。
  • 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;
    }
}
于 2013-08-31T17:19:51.333 回答
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
于 2013-08-22T05:07:23.343 回答
0

使用服务器端方法根据您的条件更改单元格的值:

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

chkValue 函数将接受两个参数并检查哪个值更大并相应地返回真/假。

于 2013-08-22T05:06:31.547 回答
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>
于 2013-08-22T05:06:37.110 回答