I am trying to populate the values of multiple rows in single row in gridview using c sharp in rowdatabound section of code behind page like:
Col1 Col6
a 1
a 2
a 3
a 4
I have changed this to
Col1 Col6
a 1
2
3
4
Now I want to achieve this, any help please?
Col1 Col6
a 1 2 3 4
What wrong with the code below?
if (e.Row.Cells[0].Text == "" && e.Row.Cells[5].Text != "")
{
for (int a = 0; a<GridView1.Rows.Count; a++)
{
string s = GridView1.Rows[a].Cells[5].Text;
GridView1.Rows[e.Row.RowIndex - 1].Cells[5].Text += s;
}
}
What mistake I am making, anyone?
Cell[0] is Col1
Cell[5] is Col6
I think you are approaching this from the wrong direction. Think of the
GridViewas your display only. You need to arrange the data as best you can before handing it off to theGridView. I am not sure how your data is stored but you might want to iterate through your data and create new records that essentially are a grouping and put all the data you need in a single row that you can databind to.The problem with using
RowDataBound(or any databinding methods) is that you are dealing with data on a row by row basis which is not what you want since you can’t ‘see’ the other rows data in it’s context.As for arranging the data, you could use the following snippet to get you the comma seperated list of for each col1 value:
This will produce a comma delimited string for a given value (
someValuein example):Use the above to make up a
Listof objects arranged so you can just bind to it directly. Eg: