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
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
Merge cell simply means merging 2 or more rows of the same value into 1. Unless you are refering to merging columns.
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
http://img403.imageshack.us/img403/8326/65829250.jpg
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 += " "; |
} |
} |
} |
Thanks
Princy.
Radgrid:
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>
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; |
} |
} |
} |
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 += " "; |
} |
} |
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");
}
}
}