Add summary to a datagridview in windows Form Application

I want to add a row in datagridview under my last row which shows summary of the records.So I Fill my dataset and then add a row in it, afterwards I Bind it to a datagridview and then when I try to assign my new row with value it gives me error that cannot convert date time to string as The fourth column datatype is datetime.SO my question is can we change column type of a specific row cell ,If no then how can I achieve what I want to do?

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

Answers


Changing the type of a particular cell could be done for some of the column types. See this answer. But I wouldn't recommend it.

It is tricky to use a row in datagridview for displaying summary as it brings in some problems (more when bound to database). You could create your summary row using textboxes outside the datagridview and add the summary data to them. Check this codeproject link for working example.


Instead of assigning the value to the cell, you could fake it by handling the CellFormatting event. If e.ColumnIndex = 4 and e.RowIndex is the last row in the grid, set the formatted value to the label you want and set the property of the EventArgs to tell it you formatted the value.

If you aren't making the whole DataGridView read-only, you probably want to also handle the CellBeginEdit event and cancel the edit if it's the summary row.


Need Your Help

How can I debug a Firefox extension with Firebug?

javascript firefox firefox-addon firebug

I'm trying to use Firebug to debug a JavaScript file in an extension I've added to Fx 4.