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
Hector
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 to share