Missing data in gridview

I created

        DataTable dt = new DataTable();

        dt.Columns.Add("Type");
        dt.Columns.Add("Address1");
        dt.Columns.Add("Address2");
        dt.Columns.Add("PostalCode");
        dt.Columns.Add("Country");

        DataRow drow = dt.NewRow();

        drow["Type"] = ddlAddressType.SelectedItem.ToString();
        drow["Address1"] = txtAddress1.Text;
        drow["Address2"] = txtAddress2.Text;
        drow["PostalCode"] = txtPostalCode.Text;
        drow["Country"] = ddlCountry.SelectedItem.ToString();

        dt.Rows.Add(drow);

        Session["Address"] = dt;

      

tried to add value to gridview using the following code:

public void populateAddressGridView()
{
    if (Session["Address"] != null)
    {
        DataTable dt = (DataTable)Session["Address"];

        if ((dt != null) && (dt.Rows.Count > 0))
        {
            AddressGridView.Visible = true;
            AddressGridView.DataSource = dt;
            AddressGridView.DataBind();
        }
        else
        {
            AddressGridView.Visible = false;
        }
    }

      

but after adding a new row of data, the fields are empty without any value. if i turn on autogeneration of fields i can view the generated data.

how can i solve this problem?

+1


source to share


4 answers


It seems to me that you need to set AutoGenerateColumns = "false" and then specify the columns to display in the gridview:



<asp:GridView ID="AddressGridView" runat="server" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField HeaderText="Type" DataField="Type" />
    <asp:BoundField HeaderText="Address1" DataField="Address1" />
    <asp:BoundField HeaderText="Address2" DataField="Address2" />
    <asp:BoundField HeaderText="PostalCode" DataField="PostalCode" />
    <asp:BoundField HeaderText="Country" DataField="Country" />
</Columns>
<EmptyDataTemplate>
    No records were found matching your search criteria
</EmptyDataTemplate>
</asp:GridView>

      

+1


source


In your GridView definition, make sure you define it if you have AutoGenerateColumns set to false.



0


source


It looks like me. When you are debugging, can you confirm that the session object appears correctly in the data table? Does it have lines after line? Is the code for the data binding method coming out?

0


source


Do I need to register

Session ["Address"] in Global.asax file before using ????

and Hector,

make sure your session is included in your web.config file.

0


source







All Articles