0

I have a gridview that is created dynamically in the back end of the code. The gridview is populated depending on the data entered into the form. When the submit button is selected the gridview is populated with the results, however when the user fills the form in the second time and selects the submit button the results in the gridview do not change. I'm not sure how I can rebind the gridview as it is dynamically created and placed in a <div> on the front end.
Below is the code I use to create the gridview

GridView gridview1 = new GridView();
            gridview1.AutoGenerateColumns = false;

            for (int i = 0; i < datasource.Columns.Count; i++)
            {
                BoundField boundfield = new BoundField();
                boundfield.DataField = datasource.Columns[i].ColumnName.ToString();
                boundfield.HeaderText = datasource.Columns[i].ColumnName.ToString();

                gridview1.Columns.Add(boundfield);
            }

            gridview1.DataSource = datasource;
            gridview1.DataBind();


            gridview.Controls.Add(gridview1);

Any help would be great.

2
Contributors
4
Replies
20
Views
2 Years
Discussion Span
Last Post by coder91
0

Hi

What is datasource and how is it populated?

The first thing that springs to mind is that you are not checking if the page is posted back so you are always recreating the GridView. Add a check for postback before applying that code:

If(!Page.IsPostBack)
{
    //your code here
}

HTH

0

datasource is a datatable that I am using to hold the data and bind it to the gridview. Because the code is dynamic and is based on stored procedures datasource is set in two different ways depending on the format that the data is sent back to the system from the db. One of the ways data is returned is with two tables, one which contains formatting information and another which contains the data, when it's this way datasource is created like

var ds = data.Tables[0].Rows[0];
            var ds1 = data.Tables[1];

            var labelArray = ds["labels"].ToString().Split(',').ToArray();
            var columnArray = ds["columns"].ToString().Split(',').ToArray();

            DataTable selectTable = ds1.DefaultView.ToTable(false, columnArray);

            //set gridview column names
            for (int i = 0; i < labelArray.Length; i++)
            {
                for (int j = 0; j < selectTable.Columns.Count; j++)
                {
                    if (i == j)
                    {
                        selectTable.Columns[j].ColumnName = labelArray[i].ToString();
                    }
                }
            }
            datasource = selectTable;

if only one table is returned it is set as

datasource = data.Tables[0];

Can I still use the If(!Page.IsPostBack)

0

Hi

I'm not 100% sure without seeing the full code whether it is actually an issue of checking if the page is posted back or if it is more of an issue of not retaining the data source.

I created the following test based on your code which simply populates the DataSource with one record the very first time and adds it to a session variable. Then when page is submitted via a button, a routine is run to add another row after first determining whether to get the DataSource from session or to create a new one. Each time however the grid is recreated. This seems to work but not sure if this answers your problem. The following is the test code:

    private DataTable datasource;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Data"] != null)
        {
            datasource = (DataTable)Session["Data"];
        }
        else
        {
            PopulateDataSource();
            Session.Add("Data", datasource);
        }

        GridView gridview1 = new GridView();
        gridview1.ID = "gridView1";
        gridview1.AutoGenerateColumns = false;

        for (int i = 0; i < datasource.Columns.Count; i++)
        {
            BoundField boundfield = new BoundField();
            boundfield.DataField = datasource.Columns[i].ColumnName.ToString();
            boundfield.HeaderText = datasource.Columns[i].ColumnName.ToString();

            gridview1.Columns.Add(boundfield);
        }

        gridview1.DataSource = datasource;
        gridview1.DataBind();

        gridview.Controls.Add(gridview1);
    }

    private void PopulateDataSource()
    {
        datasource = new DataTable();
        datasource.Columns.Add("Column 1", typeof(string));
        datasource.Columns.Add("Column 2", typeof(string));

        DataRow row = datasource.NewRow();
        row["Column 1"] = "Value 1";
        row["Column 2"] = "Value 2";

        datasource.Rows.Add(row);
    }
    protected void button1_Click(object sender, EventArgs e)
    {
        DataRow row = datasource.NewRow();
        row["Column 1"] = "Value 3";
        row["Column 2"] = "Value 4";

        datasource.Rows.Add(row);

        GridView theGridView = gridview.FindControl("gridView1") as GridView;
        theGridView.DataBind();
    }
0

Thank you for the help. I actually got it to work by removing an if else statement that was in my code.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.