【问题标题】:Add summary to a datagridview in windows Form Application在 Windows 窗体应用程序中将摘要添加到 datagridview
【发布时间】:2014-01-26 12:51:25
【问题描述】:

我想在最后一行显示记录摘要的 datagridview 中添加一行。所以我填充我的数据集,然后在其中添加一行,然后将其绑定到 datagridview,然后当我尝试分配我的具有值的新行给了我错误,无法将日期时间转换为字符串,因为第四列数据类型是日期时间。所以我的问题是我们可以更改特定行单元格的列类型,如果没有,那么我该如何实现我想要做的?

  string SelectGroupQuery = "Select * From GroupMembers Where GID=@Id ";
                            using (SqlConnection conGroup = new SqlConnection(ConnectionString.ToString()))
                            {
                                using (SqlCommand commandGroup = new SqlCommand(SelectGroupQuery, conGroup))
                                {
                                    commandGroup.CommandType = CommandType.Text;
                                    commandGroup.Parameters.Add(new SqlParameter("Id", Id));
                                    SqlDataAdapter da = new SqlDataAdapter(commandGroup);
                                    DataSet ds = new DataSet();
                                    da.Fill(ds);
                                    d1 = new DataGridView();
                                    this.Controls.Add(d1);
                                    d1.Location = new Point(50,y);
                                    d1.Size = new Size(600, 300);
                                    dr = ds.Tables[0].NewRow();
                                    ds.Tables[0].Rows.Add(dr);
                                    d1.DataSource = ds.Tables[0];
                                    d1.Columns[4].ValueType = typeof(string);
                                    d1.Rows[d1.Rows.Count-2].Cells[4].Value = "Total Amount";
                                    y = y + 400;
                                }
                            }

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    可以为某些列类型更改特定单元格的类型。请参阅this 答案。但我不会推荐它。

    datagridview 中使用一行来显示摘要是很棘手的,因为它会带来一些问题(绑定到数据库时会更多)。您可以使用 datagridview 外部的文本框创建摘要行并将摘要数据添加到其中。检查this codeproject 链接以获取工作示例。

    【讨论】:

      【解决方案2】:

      您可以通过处理 CellFormatting 事件来伪造它,而不是将值分配给单元格。如果 e.ColumnIndex = 4 并且 e.RowIndex 是网格中的最后一行,请将格式化值设置为您想要的标签并设置 EventArgs 的属性以告诉它您已格式化该值。

      如果您没有将整个 DataGridView 设为只读,您可能还需要处理 CellBeginEdit 事件并在它是摘要行时取消编辑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多