c# asp.net- gridview add a new column

Hi,
I have a gridview and I have put some footer text in, but I want to also add another row at the bottom above the footer to display some data that I am retrieving seperate to filling the gridview the first time round.
I need some help adding the row and filling it with my data
Thanks
public void DateFilter(object sender, EventArgs e)
    {
        string projectID = project.SelectedValue;
 
        string dateDayTo = toDayDate.SelectedItem.ToString();
        string dateMonthTo = toMonthDate.SelectedItem.ToString();
        string dateYearTo = toYearDate.SelectedItem.ToString();
        string dateDayFrom = fromDayDate.SelectedItem.ToString();
        string dateMonthFrom = fromMonthDate.SelectedItem.ToString();
        string dateYearFrom = fromYearDate.SelectedItem.ToString();
        SqlCommand commandviewprojects = null;
        string To = "01/01/2070";
        string From = "01/01/2000";
 
       
        if (!String.IsNullOrEmpty(toDayDate.SelectedValue) && !String.IsNullOrEmpty(toMonthDate.SelectedValue))
        {
             To = dateDayTo + "/" + dateMonthTo + "/" + dateYearTo;
            
        }
        if (!String.IsNullOrEmpty(fromDayDate.SelectedValue) && !String.IsNullOrEmpty(fromMonthDate.SelectedValue))
        {
            From = dateDayFrom + "/" + dateMonthFrom + "/" + dateYearFrom;
        }
       
        
        DBConnect.OpenConnection();
 
            DateTime dateTo = Convert.ToDateTime(To);
            DateTime dateFrom = Convert.ToDateTime(From);
 
            commandviewprojects = new SqlCommand("exec usp_GetFilteredProjectView @projectID, @dateTo, @dateFrom", DBConnect.conn);
 
            commandviewprojects.Parameters.Add("projectID", SqlDbType.UniqueIdentifier);
            commandviewprojects.Parameters["projectID"].Value = new Guid(projectID);
            commandviewprojects.Parameters.Add("dateTo", SqlDbType.DateTime);
            commandviewprojects.Parameters["dateTo"].Value = dateTo;
            commandviewprojects.Parameters.Add("dateFrom", SqlDbType.DateTime);
            commandviewprojects.Parameters["dateFrom"].Value = dateFrom;
 
       
        if (commandviewprojects != null)
        {
            gvwProject.DataSource = commandviewprojects.ExecuteReader();
 
            gvwProject.DataBind();
 
            DBConnect.CloseConnection();
        }
    } 
 
  float total;
   
 
    public void sumRows(Object src, GridViewRowEventArgs e)
    {
      
 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TableCellCollection cells = e.Row.Cells;
            total += Single.Parse(float.Parse(cells[cells.Count - 1].Text.Split(' ')[0]).ToString());
            cells[cells.Count - 1].Text = string.Format("{0:n2}",
            float.Parse(cells[cells.Count - 1].Text));
        } 
 
 
    }
 
    public void setFooter(Object src, GridViewRowEventArgs e)
    {
          string timeRemain = Convert.ToString(MinutesForProject());
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            TableCellCollection cells = e.Row.Cells;
            cells[0].Text = "";
            cells[1].ColumnSpan =1;
            cells[1].Text = "Total";
            cells[2].Text = string.Format("{0:n2}", total);
            cells[2].BackColor = System.Drawing.Color.Orange;
            
        }
       //this bit is where i'm trying to add the new row and fill it with data, not sure as i'm doing it right
        int RCount = (Convert.ToInt32(gvwProject.Rows.Count -1));
        DataTable dataT = new DataTable();
        dataT.Columns.Add("Column");
        dataT.LoadDataRow(new object[timeRemain]);
 
    }

Open in new window

CharlieDevAsked:
Who is Participating?
 
burakiewiczConnect With a Mentor Commented:
you need to create a new row then get the grdiview as a table and add it
//Do this in your gridview rowdatabound event
if (gvr.RowType == DataControlRowType.Footer)
{
      GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
            row.CssClass = "subtotalFooter";
 
            TableCell[] cells = CreateCells(gvr);
            //Build all the cell text 0 is an example  
            cells[0].Text = Subtotal";
            
            //Add the two Columns
            row.Cells.AddRange(cells);
            //get a reference to the table that holds this row
            Table tbl = (gvr.Parent as Table);
            //Add the row at the end of the list, but before the footer.
            tbl.Rows.AddAt(Gridview1.Rows.Count + 1, row);      
}
 
      private TableCell[] CreateCells(GridViewRow gvr)
        {
              TableCell[] cells = new TableCell[gvr.Cells.Count];
            for (Int32 j = 0; j < gvr.Cells.Count; j++)
            {
                cells[j] = new TableCell();
                cells[j].HorizontalAlign = gvr.Cells[j].HorizontalAlign;
                cells[j].CssClass = gvr.Cells[j].CssClass;
            }
           return cells;
        }

Open in new window

0
 
burakiewiczCommented:
CreateCells is a different method then the databound stuff
0
 
CharlieDevAuthor Commented:
Thanks, what do I define gvr as?
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
burakiewiczCommented:
oops i took that from a method i use. gvr = e.Row if using the onrowdatabound
0
 
CharlieDevAuthor Commented:
Thanks, do i define it as an object?
0
 
burakiewiczCommented:
if you want to use the code with just
GridViewRow gvr=e.Row;
or just replace
gvr with e.Row
0
 
CharlieDevAuthor Commented:
Thanks again, i have an error with tthe line  if (e.Row = DataControlRowType.Footer)
error is:
Property or indexer 'System.Web.UI.WebControls.GridViewRowEventArgs.Row' cannot be assigned to -- it is read only

Any ideas?
Thanks :)
0
 
burakiewiczCommented:
you need 1 more =
if(e.Row == DataControlRowType.Footer)
0
 
CharlieDevAuthor Commented:
It complianed about me having == hence the removal of one!
Operator '==' cannot be applied to operands of type 'System.Web.UI.WebControls.GridViewRow' and 'System.Web.UI.WebControls.DataControlRowType'

help!! :)
0
 
burakiewiczCommented:
didnt notice the rowtype missing
if(e.Row.RowType  == DataControlRowType.Footer)
like
if (gvr.RowType == DataControlRowType.Footer)
0
 
CharlieDevAuthor Commented:
great thanks:)
I have an error in my code now ,i need to fix before i can see if I need anymore help with your code
0
 
burakiewiczCommented:
ok, just let me know if you need anything
0
 
CharlieDevAuthor Commented:
Thanks for all your help :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.