简体   繁体   中英

Merge several Columns of a Gridview based on their header values using C#

I have a gridview which looks something like 这个 .

I am trying to combine multiple columns in a gridview into a single column based on it's header value.

I am trying to combine Street Name, City, State, Zipcode under one value of Address so that I get the value as a single column Address . That would reduce my total column number to just 3 in the above example with ID, Person and Address. How can I do that in C#?

I am not using BoundField/Template field instead, reading from excel file and loading it's data and binding it into a gridview so I do not know how many columns I might have in each run. I saw many tutorials which focuses on Boundfield data and using DataBinder.Eval() Method to accomplish this but this does not work for me.

My gridview simply looks like this :

<asp:GridView ID="GridView1" runat="server" HorizontalAlign="Center"> </asp:GridView>

My code behind is basically using FileUpload Control to upload excel file, establishing OleDBConnection, Creating a new DataTable and binding everything into GridView1.

How then is it possible to get them under a single column using C#? Any idea/help would be greatly appreciated.

PS: The "..." values on the last and secondlast row of the gridview are other values. This is just to show there could be hundreds of values.

My code behind is basically using FileUpload Control to upload excel file, establishing OleDBConnection, Creating a new DataTable and binding everything into GridView1.

When you Create your Datatable add new column called Address, foreach Row in your Datatable populate the "Address" Column with the desired value (Using For example a String.concat(...)).

After that you can edit your asp code like below:

<asp:GridView ID="GridView1" runat="server" HorizontalAlign="Center">
<columns>
    <!-- all the fields from the DT you want to show-->
    <asp:BoundField DataField="Id" Visible="true"></asp:BoundField>
    <asp:BoundField DataField="Person" Visible="true"></asp:BoundField>
    <asp:BoundField DataField="Address" Visible="true"></asp:BoundField>
    <asp:BoundField DataField="Other_Column_You_Like_in_dt" Visible="true"></asp:BoundField>
</columns>
</asp:GridView>

You can use String.concat method to concat 4 columns to 1 columns example :

ID:1 Adress:111 east st ; Loneville ;TX;77011

In this i have used ; to spilt and you can replace them with other signal.And when you want to reqest data from this column use this code(That is only my idea):

string[] adress=person_datagrid.Rows[adress].Cells[number cell].Value.ToString().Split(';');
 string street_name=adress[0];
 string city=adress[1];
 string state=adress[2];
 string Zipcode=adress[3];

You can use something like this: (untested). You may use StringBuilder to concatenate text and have check for nullorempty string

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
    TableCell cell = e.Row.Cells[2];
     cell.Text = cell.Text + “ “ + e.Row.Cells[3] + “ “ + e.Row.Cells[4] + “ “ + e.Row.Cells[5];
    cell.ColumnSpan = 4;
    e.Row.Cells[3].Visible = false;
     e.Row.Cells[4].Visible = false;
     e.Row.Cells[5].Visible = false;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM