【问题标题】:How to change DataGridView column value based on condition如何根据条件更改 DataGridView 列值
【发布时间】:2013-08-22 04:59:29
【问题描述】:

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

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

DataGridView 的输入来自 SQL Server。

Datagridview 的第一列是 A,第二列是 B,第三列是查找 A 是否大于 B。如果条件满足,它应该在第三列显示文本"TRUE""FALSE"

【问题讨论】:

    标签: c# winforms datagrid datagridview ado.net


    【解决方案1】:

    我在这里Changing Cell Values of DataGridView回答了类似的问题

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

    下面的示例代码假设:

    • 值是int in DataGridView:您需要进行适当的转换。
    • 没有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;
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • +1 以快速回复我对 Windows 表单的任何需要。我可以将其用于我未来的目的..
      【解决方案3】:

      试试这个

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

      【讨论】:

      • +1 以快速回复我对 Windows 表单的任何需要。我可以将其用于我未来的目的..
      【解决方案4】:
       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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-30
        • 2013-12-27
        • 2022-01-25
        • 2013-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多