protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
AddDefaultFirstRecord();
}
}
protected void AddProduct_Click(object sender, EventArgs e)
{
AddNewRecordRowToGrid();
}
private void AddDefaultFirstRecord()
{
// creating dataTable
DataTable dt = new DataTable();
DataRow dr;
dt.TableName = "AuthorBooks";
dt.Columns.Add(new DataColumn("AuthorName", typeof(string)));
dt.Columns.Add(new DataColumn("book_name", typeof(string)));
dt.Columns.Add(new DataColumn("book_type", typeof(string)));
dt.Columns.Add(new DataColumn("publisher", typeof(string)));
dr = dt.NewRow();
dt.Rows.Add(dr);
//saving databale into viewstate
ViewState["AuthorBooks"] = dt;
//bind Gridview
GridView1.DataSource = dt;
GridView1.DataBind();
}
private void AddNewRecordRowToGrid()
{
// check view state is not null
if (ViewState["AuthorBooks"] != null)
{
//get datatable from view state
DataTable dtCurrentTable = (DataTable)ViewState["AuthorBooks"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//add each row into data table
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["AuthorName"] = Textbox1.Text;
drCurrentRow["book_name"] = Textbox4.Text;
drCurrentRow["book_type"] = Textbox5.Text;
drCurrentRow["publisher"] = Textbox3.Text;
}
//Remove initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();
}
//add created Rows into dataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Save Data table into view state after creating each row
ViewState["AuthorBooks"] = dtCurrentTable;
//Bind Gridview with latest Row
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
}
// Add the Cross header
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
if(gv != null)
{
// The row to be added as the header container
GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
// The cross header
TableCell crossHeader = new TableHeaderCell();
crossHeader.ColumnSpan = gv.Columns.Count;
crossHeader.Text = "Cross Header";
row.Cells.Add(crossHeader);
// Get the table of the grid view and add the row on the top
Table t = gv.Controls[0] as Table;
if (t!= null)
{
t.Rows.AddAt(0, row);
}
}
}
Demo:
Hope this can help you.
Best regards,
Sean
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
sir your code runs well but a little problem when open this
webpage an message display No products added like using
gridview empty text then we click on button and added entries in
gridview
OK, I understand what you want now. It looks like you have added some codes behind to simulation this. However, you don't need to write those codes. Simply call the databind method and turn on "ShowHeaderWhenEmpty" property of the Grid view
with setting "EmptyDataTemplate".
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
AddDefaultFirstRecord();
}
}
protected void AddProduct_Click(object sender, EventArgs e)
{
AddNewRecordRowToGrid();
}
private void AddDefaultFirstRecord()
{
// creating dataTable
DataTable dt = new DataTable();
dt.TableName = "AuthorBooks";
dt.Columns.Add(new DataColumn("AuthorName", typeof(string)));
dt.Columns.Add(new DataColumn("book_name", typeof(string)));
dt.Columns.Add(new DataColumn("book_type", typeof(string)));
dt.Columns.Add(new DataColumn("publisher", typeof(string)));
//saving databale into viewstate
ViewState["AuthorBooks"] = dt;
//bind Gridview
GridView1.DataSource = dt;
GridView1.DataBind();
}
private void AddNewRecordRowToGrid()
{
// check view state is not null
if (ViewState["AuthorBooks"] != null)
{
//get datatable from view state
DataTable dtCurrentTable = (DataTable)ViewState["AuthorBooks"];
//add data to datatable directly without dealing with the "empty" data row
DataRow drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["AuthorName"] = Textbox1.Text;
drCurrentRow["book_name"] = Textbox4.Text;
drCurrentRow["book_type"] = Textbox5.Text;
drCurrentRow["publisher"] = Textbox3.Text;
//add created Rows into dataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Save Data table into view state after creating each row
ViewState["AuthorBooks"] = dtCurrentTable;
//Bind Gridview with latest Row
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
// Add the Cross header
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
if (gv != null)
{
// The row to be added as the header container
GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
// The cross header
TableCell crossHeader = new TableHeaderCell();
crossHeader.ColumnSpan = gv.Columns.Count;
crossHeader.Text = "Cross Header";
row.Cells.Add(crossHeader);
// Get the table of the grid view and add the row on the top
Table t = gv.Controls[0] as Table;
if (t != null)
{
t.Rows.AddAt(0, row);
}
}
}
Demo:
Hope this can help you.
Best regards,
Sean
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
sir you done well but a little mistake can we hide blue color header also before button click and simple display like used bootstrap cards sorry not data this is my requirement
One of solutions is that you could use inline codes for the webforms aspx page and remove all related settings for empty template of the grid view control.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
AddDefaultFirstRecord();
}
}
protected void AddProduct_Click(object sender, EventArgs e)
{
AddNewRecordRowToGrid();
}
private void AddDefaultFirstRecord()
{
// creating dataTable
DataTable dt = new DataTable();
dt.TableName = "AuthorBooks";
dt.Columns.Add(new DataColumn("AuthorName", typeof(string)));
dt.Columns.Add(new DataColumn("book_name", typeof(string)));
dt.Columns.Add(new DataColumn("book_type", typeof(string)));
dt.Columns.Add(new DataColumn("publisher", typeof(string)));
//saving databale into viewstate
ViewState["AuthorBooks"] = dt;
//bind Gridview
GridView1.DataSource = dt;
GridView1.DataBind();
}
private void AddNewRecordRowToGrid()
{
// check view state is not null
if (ViewState["AuthorBooks"] != null)
{
//get datatable from view state
DataTable dtCurrentTable = (DataTable)ViewState["AuthorBooks"];
//add data to datatable
DataRow drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["AuthorName"] = Textbox1.Text;
drCurrentRow["book_name"] = Textbox4.Text;
drCurrentRow["book_type"] = Textbox5.Text;
drCurrentRow["publisher"] = Textbox3.Text;
//add created Rows into dataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Save Data table into view state after creating each row
ViewState["AuthorBooks"] = dtCurrentTable;
//Bind Gridview with latest Row
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
// Add the Cross header when the row count is greater than 0
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
if (gv != null)
{
if(gv.Rows.Count > 0)
{
// The row to be added as the header container
GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
// The cross header
TableCell crossHeader = new TableHeaderCell();
crossHeader.ColumnSpan = gv.Columns.Count;
crossHeader.Text = "Cross Header";
row.Cells.Add(crossHeader);
// Get the table of the grid view and add the row on the top
Table t = gv.Controls[0] as Table;
if (t != null)
{
t.Rows.AddAt(0, row);
}
}
}
}
Demo:
Best regards,
Sean
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
55 Points
191 Posts
want to display gridview header only one time ?
Sep 07, 2020 11:27 AM|prabhjot1313|LINK
Contributor
2900 Points
852 Posts
Re: want to display gridview header only one time ?
Sep 08, 2020 09:38 AM|Sean Fang|LINK
Hi prabhjot1313,
I think you misunderstand the use of the grid view.
The Columns represent each column in one row. You should not put a table inside it -- at least for your purpose.
The correct way would be adding a line of the book information as a cell inside an item template with a header.
Apart from that, you could manually add a cross header above all of data.
More details, you could refer to below codes:
aspx:
Code behind:
Demo:
Hope this can help you.
Best regards,
Sean
Member
55 Points
191 Posts
Re: want to display gridview header only one time ?
Sep 09, 2020 08:28 AM|prabhjot1313|LINK
sir your code runs well but a little problem when open this webpage an message display No products added like using gridview empty text then we click on button and added entries in gridview
Contributor
2900 Points
852 Posts
Re: want to display gridview header only one time ?
Sep 10, 2020 08:31 AM|Sean Fang|LINK
Hi prabhjot1313,
OK, I understand what you want now. It looks like you have added some codes behind to simulation this. However, you don't need to write those codes. Simply call the databind method and turn on "ShowHeaderWhenEmpty" property of the Grid view with setting "EmptyDataTemplate".
You could refer to below codes for more details:
aspx Page:
Code behind:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { AddDefaultFirstRecord(); } } protected void AddProduct_Click(object sender, EventArgs e) { AddNewRecordRowToGrid(); } private void AddDefaultFirstRecord() { // creating dataTable DataTable dt = new DataTable(); dt.TableName = "AuthorBooks"; dt.Columns.Add(new DataColumn("AuthorName", typeof(string))); dt.Columns.Add(new DataColumn("book_name", typeof(string))); dt.Columns.Add(new DataColumn("book_type", typeof(string))); dt.Columns.Add(new DataColumn("publisher", typeof(string))); //saving databale into viewstate ViewState["AuthorBooks"] = dt; //bind Gridview GridView1.DataSource = dt; GridView1.DataBind(); } private void AddNewRecordRowToGrid() { // check view state is not null if (ViewState["AuthorBooks"] != null) { //get datatable from view state DataTable dtCurrentTable = (DataTable)ViewState["AuthorBooks"]; //add data to datatable directly without dealing with the "empty" data row DataRow drCurrentRow = dtCurrentTable.NewRow(); drCurrentRow["AuthorName"] = Textbox1.Text; drCurrentRow["book_name"] = Textbox4.Text; drCurrentRow["book_type"] = Textbox5.Text; drCurrentRow["publisher"] = Textbox3.Text; //add created Rows into dataTable dtCurrentTable.Rows.Add(drCurrentRow); //Save Data table into view state after creating each row ViewState["AuthorBooks"] = dtCurrentTable; //Bind Gridview with latest Row GridView1.DataSource = dtCurrentTable; GridView1.DataBind(); } } // Add the Cross header protected void GridView1_DataBound(object sender, EventArgs e) { GridView gv = (GridView)sender; if (gv != null) { // The row to be added as the header container GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal); // The cross header TableCell crossHeader = new TableHeaderCell(); crossHeader.ColumnSpan = gv.Columns.Count; crossHeader.Text = "Cross Header"; row.Cells.Add(crossHeader); // Get the table of the grid view and add the row on the top Table t = gv.Controls[0] as Table; if (t != null) { t.Rows.AddAt(0, row); } } }
Demo:
Hope this can help you.
Best regards,
Sean
Member
55 Points
191 Posts
Re: want to display gridview header only one time ?
Sep 11, 2020 05:38 AM|prabhjot1313|LINK
sir you done well but a little mistake can we hide blue color header also before button click and simple display like used bootstrap cards sorry not data this is my requirement
Contributor
2900 Points
852 Posts
Re: want to display gridview header only one time ?
Sep 11, 2020 08:40 AM|Sean Fang|LINK
Hi prabhjot1313,
One of solutions is that you could use inline codes for the webforms aspx page and remove all related settings for empty template of the grid view control.
Please refer to below codes:
aspx
Code behind
Demo:
Best regards,
Sean