Answered by:
to clean an empty value of field in gridview inside a Listview in codebehind

Question
-
User-1497429768 posted
Hi,
I put a gridview inside a Listview control's ItemTemplate.The Listview and gridview can show all of the data normally.The gridview has five fields.
My question is if the field-A of the gridview is zero,I want to clean the field-A、field-B and field-C to empty value and merge the three field to one field and put some word in the merged field in codebehind.How to do that? Thanks.
Saturday, October 28, 2017 1:56 AM
Answers
-
User-1838255255 posted
Hi jeff.wenchai,
According to your description, you want to merge the columns through column one filed value:
Sample Code:
<head runat="server"> <title></title> <style type="text/css"> body { font-family: Arial; font-size: 10pt; } </style> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" /> <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" /> </Columns> </asp:GridView> </form> </body> using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), new DataColumn("Name", typeof(string)), new DataColumn("Country",typeof(string)) }); dt.Rows.Add(0, "John Hammond", "United States"); dt.Rows.Add(2, "Mudassar Khan", "India"); dt.Rows.Add(0, "Suzanne Mathews", "France"); dt.Rows.Add(4, "Robert Schidner", "Russia"); GridView1.DataSource = dt; GridView1.DataBind(); } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { TableCell cell = e.Row.Cells[0]; int quantity = int.Parse(cell.Text); if (quantity == 0) { e.Row.Cells[0].ColumnSpan = 3; e.Row.Cells[0].Text = "Code"; e.Row.Cells.RemoveAt(2); e.Row.Cells.RemoveAt(1); } } } }
Result:
Best Regards,
Eric Du
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 30, 2017 8:32 AM
All replies
-
User-1838255255 posted
Hi jeff.wenchai,
According to your description, you want to merge the columns through column one filed value:
Sample Code:
<head runat="server"> <title></title> <style type="text/css"> body { font-family: Arial; font-size: 10pt; } </style> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" /> <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" /> </Columns> </asp:GridView> </form> </body> using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), new DataColumn("Name", typeof(string)), new DataColumn("Country",typeof(string)) }); dt.Rows.Add(0, "John Hammond", "United States"); dt.Rows.Add(2, "Mudassar Khan", "India"); dt.Rows.Add(0, "Suzanne Mathews", "France"); dt.Rows.Add(4, "Robert Schidner", "Russia"); GridView1.DataSource = dt; GridView1.DataBind(); } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { TableCell cell = e.Row.Cells[0]; int quantity = int.Parse(cell.Text); if (quantity == 0) { e.Row.Cells[0].ColumnSpan = 3; e.Row.Cells[0].Text = "Code"; e.Row.Cells.RemoveAt(2); e.Row.Cells.RemoveAt(1); } } } }
Result:
Best Regards,
Eric Du
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 30, 2017 8:32 AM -
User-1497429768 posted
Hi Eric Du,
It's really amazing. Thanks for your help.
best regards,
jeff
Tuesday, October 31, 2017 8:58 AM