This is a migrated thread and some comments may be shown as answers.

Gridview code to Radgrid

10 Answers 312 Views
Grid
This is a migrated thread and some comments may be shown as answers.
L
Top achievements
Rank 1
L asked on 09 Jul 2009, 05:01 AM
hi

I have this GridView code and i have tried using this in Radgrid. it seems not working.

how do i convert this Gridview code to be used in Radgrid. Thanks

public class GridDecorator
{
    public static void MergeRows(GridView gridView)
    {
        for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
        {
            GridViewRow row = gridView.Rows[rowIndex];
            GridViewRow previousRow = gridView.Rows[rowIndex + 1];

            for (int i = 0; i < row.Cells.Count; i++)
            {
                if (row.Cells[i].Text == previousRow.Cells[i].Text)
                {
                    row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 : previousRow.Cells[i].RowSpan + 1;
                    previousRow.Cells[i].Visible = false;
                }
            }
        }
    }
}


protected void gridView_PreRender(object sender, EventArgs e)
{
    GridDecorator.MergeRows(gridView);
}

10 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 09 Jul 2009, 06:48 AM
Hi,

Here is the equivalent code for RadGrid.

CS:
 
 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        GridDecorator.MergeRows(RadGrid1); 
 
    } 
 
      public class GridDecorator 
      { 
          public static void MergeRows(RadGrid RadGrid1) 
        { 
            for (int rowIndex = RadGrid1.Items.Count - 2; rowIndex >= 0; rowIndex--) 
            { 
                GridDataItem row = RadGrid1.Items[rowIndex]; 
                GridDataItem previousRow = RadGrid1.Items[rowIndex + 1]; 
 
                for (int i = 0; i < row.Cells.Count; i++) 
                { 
                    if (row.Cells[i].Text == previousRow.Cells[i].Text) 
                    { 
                        row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 : previousRow.Cells[i].RowSpan + 1; 
                        previousRow.Cells[i].Visible = false
                    } 
                } 
            } 
        } 
     } 


Regards
Shinu
0
Trung Nguyen
Top achievements
Rank 1
answered on 10 Jul 2009, 10:20 AM
this is merge cell, how to merge row, help me...plz
0
L
Top achievements
Rank 1
answered on 10 Jul 2009, 01:57 PM
hi

Merge cell simply means merging 2 or more rows of the same value into 1. Unless you are refering to merging columns.
0
Trung Nguyen
Top achievements
Rank 1
answered on 11 Jul 2009, 02:26 AM
hi
i have code to merge row in radgird :
for (int i = RadGrid1.Items.Count - 2 ; i > 0; i--) 
            { 
                if (RadGrid1.Items[i][RadGrid1.Columns[0]].Text == RadGrid1.Items[i-1][RadGrid1.Columns[0]].Text) 
                { 
                    RadGrid1.Items[i-1][RadGrid1.Columns[0]].RowSpan = RadGrid1.Items[i][RadGrid1.Columns[0]].RowSpan < 2 ? 2 : RadGrid1.Items[i][RadGrid1.Columns[0]].RowSpan + 1; 
                    RadGrid1.Items[i][RadGrid1.Columns[0]].Visible = false
                } 
            } 

but not border, how to border cell? help me plz


0
Trung Nguyen
Top achievements
Rank 1
answered on 11 Jul 2009, 02:29 AM
result:
http://img403.imageshack.us/img403/8326/65829250.jpg
0
Princy
Top achievements
Rank 2
answered on 13 Jul 2009, 06:48 AM
Hello,

I tried your code to merge cells with equal values, at my end for different skins and it does not affect the border lines for cells in the grid. I'm not sure of whats causing this issue at your end but probably you can work around this by adding the following code and let me know if it helps:

c#:
protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        foreach (GridDataItem dataItem in RadGrid1.Items) 
        { 
            foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) 
            { 
                if (dataItem[col.UniqueName].Text == string.Empty) 
                    dataItem[col.UniqueName].Text += "&nbsp"
 
            } 
        }  
    } 

Thanks
Princy.
0
Trung Nguyen
Top achievements
Rank 1
answered on 14 Jul 2009, 01:44 AM
Thanks Princy , I have tried using this code but it seems not working.

Radgrid:
<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Office2007"
        AutoGenerateColumns="False" onneeddatasource="RadGrid1_NeedDataSource"
        onitemdatabound="RadGrid1_ItemDataBound" onprerender="RadGrid1_PreRender"
        onitemcreated="RadGrid1_ItemCreated">        
            <itemstyle borderstyle="Solid" />
            <mastertableview datakeynames="ID_Cuoc_Hop,TT">
                <RowIndicatorColumn>
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn>
                    <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                <Columns>                                        
                    <telerik:GridBoundColumn HeaderText="Thứ /Ngày" DataField="THU_NGAY">
                        <HeaderStyle Width="8%" Height="25px" HorizontalAlign="Center" Font-Bold="true"></HeaderStyle>
                        <ItemStyle Height="35px" HorizontalAlign="Center"></ItemStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="T.Gian" DataField="GIO_PHUT">
                        <HeaderStyle Width="5%" Height="25px" HorizontalAlign="Center" Font-Bold="true"></HeaderStyle>
                        <ItemStyle Height="35px" HorizontalAlign="Center"></ItemStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="Địa điểm" DataField="TEN_PHONG">
                        <HeaderStyle Width="12%" Height="25px" HorizontalAlign="Center" Font-Bold="true"></HeaderStyle>
                        <ItemStyle Height="35px" HorizontalAlign="Center"></ItemStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="Nội dung" DataField="NOI_DUNG">
                        <HeaderStyle Width="23%" Height="25px" HorizontalAlign="Center" Font-Bold="true"></HeaderStyle>
                        <ItemStyle Height="35px" HorizontalAlign="Left"></ItemStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="Chuẩn bị" DataField="CHUAN_BI">
                        <HeaderStyle Width="14%" Height="25px" HorizontalAlign="Center" Font-Bold="true"></HeaderStyle>
                        <ItemStyle Height="35px" HorizontalAlign="Left"></ItemStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="Chủ trì" DataField="CHU_TRI">
                        <HeaderStyle Width="12%" Height="25px" HorizontalAlign="Center" Font-Bold="true"></HeaderStyle>
                        <ItemStyle Height="35px" HorizontalAlign="Left"></ItemStyle>
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="Thành phần" DataField="THANH_PHAN">
                        <HeaderStyle Width="20%" Height="25px" HorizontalAlign="Center" Font-Bold="true"></HeaderStyle>
                        <ItemStyle Height="35px" HorizontalAlign="Left"></ItemStyle>
                    </telerik:GridBoundColumn>                    
                    <telerik:GridTemplateColumn HeaderText="Tình Trạng" UniqueName="TT" ReadOnly="true">
                        <HeaderStyle Width="5%" Height="25px" HorizontalAlign="Center" Font-Bold="true"></HeaderStyle>
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblStatus" Text='<%# Bind("TT") %>' Visible="false"></asp:Label>
                        </ItemTemplate>                        
                    </telerik:GridTemplateColumn>                     
                </Columns>                
            </mastertableview>
            <filtermenu enabletheming="True">
                <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
            </filtermenu>
    </telerik:RadGrid>
Method to merger row

public static void MergeRows(RadGrid RadGrid1) 
        { 
            for (int i = RadGrid1.Items.Count - 2; i > 0; i--) 
            { 
                if (RadGrid1.Items[i][RadGrid1.Columns[0]].Text == RadGrid1.Items[i - 1][RadGrid1.Columns[0]].Text) 
                { 
                    RadGrid1.Items[i - 1][RadGrid1.Columns[0]].RowSpan = RadGrid1.Items[i][RadGrid1.Columns[0]].RowSpan < 2 ? 2 : RadGrid1.Items[i][RadGrid1.Columns[0]].RowSpan + 1; 
                    RadGrid1.Items[i][RadGrid1.Columns[0]].Visible = false;                     
                } 
            }             
        } 
     RadGrid Prerender of Princy.
MergeRows(RadGrid1); 
            foreach (GridDataItem dataItem in RadGrid1.Items) 
            { 
                foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) 
                { 
                    if (dataItem[col.UniqueName].Text == string.Empty) 
                        dataItem[col.UniqueName].Text += "&nbsp"; 
 
                } 
            }   
0
Trung Nguyen
Top achievements
Rank 1
answered on 17 Jul 2009, 03:43 AM
help me please...thanks.
0
Trung Nguyen
Top achievements
Rank 1
answered on 26 Aug 2009, 06:30 AM
help me?
0
nisha
Top achievements
Rank 1
answered on 17 Jun 2010, 10:35 AM
Hi,

If I use this code, i'm getting error saying
'Failed accessing GridColumn by index. Please verify that you have specified the structure of RadGrid correctly'.
Any help would be appreciated.
Code i'm using is to merge cells is,

 

public class GridDecorator

 

    {

 

         

public static void MergeRows(RadGrid RadGrid1)

 

             {

                 

for (int i = RadGrid1.Items.Count - 2; i > 0; i--)

 

                     {

                         

if (RadGrid1.Items[i][RadGrid1.Columns[0]].Text == RadGrid1.Items[i - 1][RadGrid1.Columns[0]].Text)

 

                                 {

                                    RadGrid1.Items[i - 1][RadGrid1.Columns[0]].RowSpan = RadGrid1.Items[i][RadGrid1.Columns[0]].RowSpan < 2 ? 2 : RadGrid1.Items[i][RadGrid1.Columns[0]].RowSpan + 1;

                                     RadGrid1.Items[i][RadGrid1.Columns[0]].Visible =

false;

 

                                   }    

                        }

                 

foreach (GridDataItem dataItem in RadGrid1.Items)

 

                     {

                         

foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns)

 

                             {

                                dataItem[col.UniqueName].Style.Add(

"border-left", "solid 1px #ededed");

 

                             }

                }

 

}

 

 

Tags
Grid
Asked by
L
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Trung Nguyen
Top achievements
Rank 1
L
Top achievements
Rank 1
Princy
Top achievements
Rank 2
nisha
Top achievements
Rank 1
Share this question
or