【问题标题】:How to change data coming from database into DataGridView如何将来自数据库的数据更改为 DataGridView
【发布时间】:2011-08-12 14:18:30
【问题描述】:

我有一个带有 MASTER 表的数据库。 MASTER 有一列 Salary,其数据类型为 MONEY。 现在,当我从 datagridview 中的数据库获取数据时,我想用“Rs”显示货币列。附加到它。 就像,如果数据库中的薪水是 100,我想将其视为卢比。 100 在我的数据网格视图中。 Fill 方法只是将整个数据原样复制到 datagridview 中。 那怎么办呢?

【问题讨论】:

    标签: c# sql-server-2008


    【解决方案1】:
    dataGridView1.CellFormatting +=
            new System.Windows.Forms.DataGridViewCellFormattingEventHandler(
            this.dataGridView1_CellFormatting);
    
    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Salary")
        {
            if (e.Value != null)
            {
                try
                {
                    e.Value = String.Format("Rs. {0:F2}", e.Value);
                    e.FormattingApplied = true;
                }
                catch (FormatException)
                {
                    e.FormattingApplied = false;
                }
            }
        }
    }
    

    【讨论】:

    • 我收到错误消息-“当前上下文中没有出现“格式化”名称。”
    • @user815600 - 抱歉,复制/粘贴错误。我更正了示例以显示 e 而不是 formatting
    • 完美。它工作正常。但它给了我喜欢,卢比。 100 美元,但如何摆脱那个 $ 符号。请多多包涵,我对这门语言和领域很陌生
    • 通过删除货币符号 c 是有效的,我的意思是让“Rs. {0:c}”到“Rs. {0}”对我来说很好。但是如果我想控制小数点怎么办。我的意思是,如果我们说 c2 它给出 2 个小数位,c3 给出 3 个小数位。从表达式中删除 c 后如何操作。
    • @user815600 - 抱歉,{0:C} 表示以货币格式将传递给String.Format() 的参数中的第一项(0 索引)格式化。我忘了你想用“卢比”替换货币。您可以将{0:F} 用于浮点数,默认为2 位小数。您可以更明确地说 {0:F2}{0:0.00} 指定至少一位数字和两位小数。
    【解决方案2】:

    使用相关列的 DefaultCellStyle 属性的 Format 属性。您可以通过右键单击 DataGridView 控件,使用“编辑列”对话框以声明方式执行此操作。在您的情况下,您需要将 Format 属性设置为 'Rs。 0.00。需要单引号来转义 Rs 后的点号。

    有关更多信息,请阅读 MSDN 上的 Custom Numeric Format Strings 主题。

    【讨论】:

      【解决方案3】:

      我不确定,但是您可以使用DataGridView.OnUserAddedRow Method修改行添加事件

      这是一个简短的代码。我没有测试过,但请检查它是否适合你:

      protected override void OnUserAddedRow(DataGridViewRowEventArgs e)
      {
          int actualRowIndex = this.Rows.Count - 2; 
          this.Rows[actualRowIndex].Cells["Salary"].Value = "Rs." + <Your SQL Salary>;
          base.OnUserAddedRow(e);
      }
      

      您可以在此处阅读有关此用法的更多信息:Dynamically set DataGridViewColumn default value at runtime

      【讨论】:

        【解决方案4】:

        在 DatabindingComplete 上,您可以手动修改该列(如果您使用的是 winforms)

            private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    row.Cells["Salary"].Value = "Add your calc here";
                }
            }
        

        【讨论】:

        • "foreach" 会起作用,但在填充 DataGridView 后修改列只是开销。
        • 我同意你的看法。实际上我一直在寻找“InitializeRow”事件,但后来我意识到我使用 Infragistics 组件的时间太长了;)
        • 实际上我在数据库中的 Salary 数据类型是 Money。当我添加“卢比”时。到我的 DatagridView,它给出了像 Input format is wrong 这样的错误。
        • 然后您必须在特定单元格上设置格式,如@Exulted 建议的进一步向下。或者您可以在数据网格中添加一个新的未绑定列“Salary2”,然后在我的建议中将“Salary”与“Salary2”交换
        • 它给出了 DataGridView 默认错误框。错误-“Rs. 100 不是十进制的有效值。输入字符串的格式不正确。”
        猜你喜欢
        • 1970-01-01
        • 2021-12-11
        • 2017-10-17
        • 2015-01-04
        • 2016-06-11
        • 1970-01-01
        • 1970-01-01
        • 2012-07-07
        • 1970-01-01
        相关资源
        最近更新 更多