Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a datagrid and inside the grid I have five columns. In one column, I am displaying comma seperated values which are unOrganized.the gridview is like

Col1          Col2          Col3                   Col4           Col5
 1             a         [acvdfvd], [dfasfsd        20              10
                         dfdfs],[sdfdsf][sadsa
                         fsfesf],[adsdas],
                         [dsfsdfdf],[dfss]

 2             b             "                       30              20

Now, I want to show the values like this,
Col1          Col2          Col3                   Col4           Col5  
 1             a          [acvdfvd],                 20              10
                          [dfasfsddfdfs],
                          [sdfdsf],
                          [sadsafsfesf],
                          [cxadsdas],
                          [dsfsdfdf],
                          [dfss]

  2             b             "                       30              20


This will atleast be understandable. Pleace help me on how to do it. Thanks.
Posted
Comments
Nandakishore G N 13-Feb-13 2:14am    
use rowdatabound..split the values for ',' and append break ('<br/>') after every comma...and reappend it to the same label

You can use nested grid for col 3 to display comma separated values.

Add gridview property : onrowdatabound="parent_RowDataBound" to parent gridview

C#
protected void parent_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Bind child gridview here...
    }
}


Try this out..
 
Share this answer
 
v2
Comments
TanvirRaihan 13-Feb-13 3:44am    
Why to use nested grid ? If the question is how to display it line by line, then on row_databound event, we can get the actual string and split it by comma(,) and then join them with a line break in between each splitted item and set the new string in grid's cell.
go through following link...
http://stackoverflow.com/questions/14456826/c-line-break-instead-of-comma-separated-values-in-gridview[^]
in that code in Literal tag use
Text='<%# string.Join("<br />,", Eval("Values").ToString().Split(new []{","},StringSplitOptions.None)) %>'

becouse u want separate values by , and they have values on next line but , is removed from values..
 
Share this answer
 
By using Repeater we can easily achieve this.

1) Build MainRepeater with your Main Data with the columns of [Col1,Col2,Col3,Col4,Col5].

2) Inside of Col3 ,Insert one nested Repeater called splitRepeater.

3) Write a ItemDatabound event of MainRepeater and Split the the Value of Col3 with Comma(,) and Find SplitRepeater and Bind the Array of Splitted String.
 
Share this answer
 
First bind string separated by comma to a label...

Then in RowDataBound...

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
               Label lblcol3 = (Label)e.Row.FindControl("lblcol3");
               string temp = lblcol3.Text;
                if (temp != "")
                {
                    string[] temporary = temp.Split(',');
                    int length = temporary.Count();
                    StringBuilder db = new StringBuilder();
                    bool first = true;
                    if (length > 0)
                    {
                        foreach (string word in temporary)
                        {
                            if (word == "," || word == "")
                            {
                                break;
                            }
                            else
                            {
                                if(!first)
                                {
                                   db.Append("," + "<br />");
                                   
                                }
                                
                                   db.Append(word);// adding              
                            }
                        }
                        lblcategory.Text = db.ToString(); // final string
                    }
                    else
                    {
                        
                    }
        }
    }


...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900