Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am storing some data into a variable at Page Load event. And I have a gridview in the same page which is getting data from SQL database. What i want is to also add another column in the gridview and populate it with the value in the variable from Page Load event.
Is there any way for this? I want to do this because i am calculating something which will be changed with respect to change in date, so therefore i want to fetch data from page load and populate the gridview with it.

What I have tried:

Searched google and other forums but could not find anything related to this.
Posted
Updated 7-Oct-16 11:38am
Comments
ZurdoDev 6-Oct-16 14:04pm    
Search for how to dynamically add a column to gridview.

A data representation control like GridView can only handle one DataSource at a time. Since your data is coming from different sources (database and code variable), then you may need create a custom DataTable that would holds all the information you need from your sources. You could then use that custom DataTable as your final DataSource for your GridView.

Here's a quick example: (PS: Never tested on this, but it should give you some idea)
C#
private void BindGrid(DataDaTable source)
{
        //create your final datasource
        DataTable dt = new DataTable();
        DataRow dr;

        //define new sets of columns that you need
        dt.Columns.Add(new System.Data.DataColumn("Column1", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Column2", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Column3", typeof(String)));
 
        //you can add more columns here if needed

        //fill your new DataTable with data from your sources
        int rowCount = source.Rows.Count;
	if ( rowCount > 0){
            for(int i=0; i < rowCount; i++){
            dr = dt.NewRow();
            dr[0] = source.Rows[i]["ColumnName"].ToString();
            dr[1] = source.Rows[i]["ColumnName"].ToString();
	    dr[2] = "Your custom variable value here";
            dt.Rows.Add(dr);
            }
        }
 
	GridView1.DataSource = dt;
        GridView1.DataBind();
}


Alternatively, you could just add a new Column to your existing DataTable something like:
C#
DataColumn newColumn = new DataColumn("ColumnName", typeof(System.String));
newColumn.DefaultValue = "Your variable value";
dt.Columns.Add(newColumn);
 
Share this answer
 
I have tried below

C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        string customColumn = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            customColumn = "INDIA";
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            table.Columns.Add("Dosage", typeof(int));
            table.Columns.Add("Drug", typeof(string));
            table.Columns.Add("Patient", typeof(string));
            table.Columns.Add("Date", typeof(DateTime));

            // Here we add five DataRows.
            table.Rows.Add(25, "Indocin", "David", DateTime.Now);
            table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
            table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
            table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
            table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);


            //Add new column  in gridview if its not set true for autogenerare columns
            //BoundField test = new BoundField();
            //test.DataField = "Country";
            //test.HeaderText = "Country";
            //GridView1.Columns.Add(test);


            //add  new column for your custom variable to datasource
            table.Columns.Add(new DataColumn("Country"));


            GridView1.DataSource = table;
            GridView1.DataBind();

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //putting data in column Conditionaly 
                if (Convert.ToDateTime(e.Row.Cells[3].Text)<DateTime.Now)
                {
                    e.Row.Cells[4].Text = customColumn;
                }
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900