Hi
GridView with merged rows in one column.
I am merging the rows in Rowdatabound event.So my GridView will look like
CATEGORY |
PRODUCT |
VALUE |
SHOES |
NIKE |
600 |
REEBOK |
700 |
CLOTHING |
PUMA |
400 |
SHIRTS |
200 |
PANTS |
800 |
Then now i need to edit the value, so i put AutogeneratedEditbutton=true.when i click edit,the gridcells of that merged row displaces
displaces.when i run it in browser and click edit in the first row, the cell0 is moved to cell1 place and cell1 is moved to cell2's place.then my grid changes as below
Appreciate your help on this.
|
CATEGORY |
PRODUCT |
VALUE |
|
edit |
SHOES |
|
NIKE |
600 |
edit |
REEBOK |
700 |
|
edit |
CLOTHING |
|
PUMA |
400 |
edit |
SHIRTS |
200 |
|
edit |
PANTS |
800 |
|
Below is the code that is being used
=======================================================
//Design .aspx code of Grid View.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
BorderStyle="Solid" Font-Size="Small" CssClass="mGrid" Width="800px"
Font-Names="Arial" CellPadding=3 BorderWidth="1px" ondatabound="GridView1_DataBound"
onrowdatabound="GridView1_RowDataBound"
onrowcreated="GridView1_RowCreated" AutoGenerateEditButton="True"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField ControlStyle-Width="10px" DataField="Category"
HeaderStyle-CssClass="cat" ItemStyle-CssClass="cat" HeaderText="Category"
SortExpression="Category" >
<ControlStyle Width="10px"></ControlStyle>
<HeaderStyle CssClass="cat"></HeaderStyle>
<ItemStyle CssClass="cat"></ItemStyle> </asp:BoundField>
<asp:BoundField DataField="subCategory" HeaderText="SubCategory"
SortExpression="subCategory">
<ControlStyle Width="25px"></ControlStyle>
<HeaderStyle CssClass="sub"></HeaderStyle>
<ItemStyle CssClass="sub"></ItemStyle></asp:BoundField>
<asp:TemplateField HeaderText="Supplier"><ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="G"></asp:ListItem>
<asp:ListItem Text="R"></asp:ListItem>
<asp:ListItem Text="A"></asp:ListItem>
</asp:DropDownList> </ItemTemplate>
<ControlStyle Width="50px"></ControlStyle></asp:TemplateField>
<asp:TemplateField HeaderText="Customer"><ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Text="G"></asp:ListItem>
<asp:ListItem Text="R"></asp:ListItem>
<asp:ListItem Text="A"></asp:ListItem>
</asp:DropDownList> </ItemTemplate>
<ControlStyle Width="50px"></ControlStyle></asp:TemplateField>
<asp:TemplateField HeaderText="Joint"><ItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server">
<asp:ListItem Text="G"></asp:ListItem>
<asp:ListItem Text="R"></asp:ListItem>
<asp:ListItem Text="A"></asp:ListItem>
</asp:DropDownList> </ItemTemplate>
<ControlStyle Width="50px"></ControlStyle></asp:TemplateField>
<asp:BoundField DataField="H" HeaderText="H" SortExpression="H"></asp:BoundField>
<asp:BoundField DataField="A" HeaderText="A" SortExpression="A"></asp:BoundField>
</Columns> </asp:GridView> </ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowUpdating" />
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowEditing" />
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCancelingEdit" />
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowDataBound" />
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCreated" />
</Triggers> </asp:UpdatePanel> </td> </tr>
<tr> <td align="center">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click1"
Text="Submit" Visible="False" />
<asp:Label ID="Label26" runat="server" Visible="False" ForeColor="#0066FF"></asp:Label>
</ContentTemplate> </asp:UpdatePanel> </td>
</tr> </table>.cs code for Gridview
protected void GridView1_DataBound(object sender, EventArgs e) {
string text = ""; int count = 0;
Hashtable ht = new Hashtable();
// loop through all rows to get row counts
foreach (GridViewRow gvr in GridView1.Rows) {
//Label l = (Label)gvr.FindControl("lblCategory");
if (gvr.RowType == DataControlRowType.DataRow) {
if (gvr.Cells[1].Text == text) {
count++; } else
{ if (count > 0) {
ht.Add(text, count); }
text = gvr.Cells[1].Text; count = 1;
} } } if (count > 1) {
ht.Add(text, count); }
// loop through all rows again to set rowspan text = "";
foreach (GridViewRow gvr in GridView1.Rows) {
//Label l = (Label)gvr.FindControl("lblCategory");
if (gvr.RowType == DataControlRowType.DataRow) {
if (gvr.Cells[1].Text == text) {
gvr.Cells.Remove(gvr.Cells[1]); }
else {
text = gvr.Cells[1].Text;
gvr.Cells[1].RowSpan = Convert.ToInt32(ht[text]);
} } } }