Answered by:
change the format of DataTable splitting the column in two

Question
-
User364607740 posted
I've a DataTable which format I need to change. The values of the DataTable are from the Database.
Current DataTable format
CUSTOMER TYPE FEE NEW_FREE NEW_PAID OLD_FREE OLD_PAID GOLD 0 NULL NULL 2 NULL BRONZE 100 NULL 1 1 1 OTHERS 200 NULL NULL 1 NULL This DataTable shows the new & free customer type having free & paid registration. But I've to change the format of this DataTable to ::
Required DataTable format
NEW OLD CUSTOMER TYPE FEE FREE PAID FREE PAID GOLD 0 NULL NULL 2 NULL BRONZE 100 NULL 1 1 1 OTHERS 200 NULL NULL 1 NULL The table top row should be to categorized as NEW/OLD customer and from the second row customer type and free and paid customer type. How can I do this?
Code so far I've done ::
CODE BEHIND ::
protected void search_Click(object sender, EventArgs e) { // code blocks if (dt != null && dt.Rows.Count > 0) { DataView dv = dt.DefaultView; // Sort data dv.Sort = "CUSTOMER TYPE DESC"; // Convert back your sorted DataView to DataTable dt = dv.ToTable(); GridView1.DataSource = dt; GridView1.DataBind(); div_error.Visible = true; DataTable_Format(dt); } } protected void DataTable_Format(DataTable temp_dt) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn() { DataType = typeof(string), ColumnName = "CUSTOMER TYPE" }); dt.Columns.Add(new DataColumn() { DataType = typeof(int), ColumnName = "FREE" }); dt.Columns.Add(new DataColumn() { DataType = typeof(int), ColumnName = "PAID" }); dt.Columns.Add(new DataColumn() { DataType = typeof(int), ColumnName = "FREE" }); dt.Columns.Add(new DataColumn() { DataType = typeof(int), ColumnName = "PAID" }); }
My above code is not correct. I've just provided the code so far I tried to do. Further I've no idea how to proceed.
How can I categorized the top row of the table as NEW & OLD and the second row as CUSTOMER TYPE, FEE, and for NEW column two columns FREE & PAID and for OLD column again two columns FREE & PAID?
Please help me. Thanks!!!
Saturday, September 24, 2016 3:27 PM
Answers
-
User-707554951 posted
Hi scala_1988,
From you description, you want to change the format of your GridView, adding a row and change Headertext. If that the case I suggest you could refer to the code below:<asp:GridView ID="GridView1" HeaderStyle-BackColor="#9AD6ED" HeaderStyle-ForeColor="#636363" runat="server" AutoGenerateColumns="false" OnDataBound = "OnDataBound"> <Columns> <asp:BoundField DataField="CustomerName" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="CustomerCountry" HeaderText="Country" ItemStyle-Width="150" /> <asp:BoundField DataField="EmployeeName" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="EmployeeCountry" HeaderText="Country" ItemStyle-Width="150" /> </Columns> </asp:GridView>
CodeBehind:
using System.Data; using System.Drawing; protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[4] { new DataColumn("CustomerName"), new DataColumn("CustomerCountry"), new DataColumn("EmployeeName"), new DataColumn("EmployeeCountry") }); dt.Rows.Add("John Hammond", "United States", "Albert Dunner", "Bolivia"); dt.Rows.Add("Mudassar Khan", "India", "Jason Sprint", "Canada"); dt.Rows.Add("Suzanne Mathews", "France", "Alfred Lobo", "Philippines"); dt.Rows.Add("Robert Schidner", "Russia", "Shaikh Ayyaz", "UAE"); GridView1.DataSource = dt; GridView1.DataBind(); } } protected void OnDataBound(object sender, EventArgs e) { GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal); TableHeaderCell cell = new TableHeaderCell(); cell.Text = "Customers"; cell.ColumnSpan = 2; row.Controls.Add(cell); cell = new TableHeaderCell(); cell.ColumnSpan = 2; cell.Text = "Employees"; row.Controls.Add(cell); row.BackColor = ColorTranslator.FromHtml("#3AC0F2"); GridView1.HeaderRow.Parent.Controls.AddAt(0, row); }
The Screenshot like below:
Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.
Best regards
Cathy- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 26, 2016 9:15 AM