[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

c# asp.net- gridview add a new column

Posted on 2009-02-24
13
Medium Priority
?
1,539 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:CharlieDev
  • 7
  • 6
13 Comments
 
LVL 16

Accepted Solution

by:
burakiewicz earned 2000 total points
ID: 23722800
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
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23722807
CreateCells is a different method then the databound stuff
0
 

Author Comment

by:CharlieDev
ID: 23723014
Thanks, what do I define gvr as?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Expert Comment

by:burakiewicz
ID: 23723098
oops i took that from a method i use. gvr = e.Row if using the onrowdatabound
0
 

Author Comment

by:CharlieDev
ID: 23723178
Thanks, do i define it as an object?
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23723196
if you want to use the code with just
GridViewRow gvr=e.Row;
or just replace
gvr with e.Row
0
 

Author Comment

by:CharlieDev
ID: 23723389
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
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23723433
you need 1 more =
if(e.Row == DataControlRowType.Footer)
0
 

Author Comment

by:CharlieDev
ID: 23723465
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
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23723503
didnt notice the rowtype missing
if(e.Row.RowType  == DataControlRowType.Footer)
like
if (gvr.RowType == DataControlRowType.Footer)
0
 

Author Comment

by:CharlieDev
ID: 23723778
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
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23723835
ok, just let me know if you need anything
0
 

Author Closing Comment

by:CharlieDev
ID: 31550575
Thanks for all your help :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question