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

RadGrid Rows Move Up/Down

5 Answers 170 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sandeep
Top achievements
Rank 1
Sandeep asked on 19 May 2012, 01:06 PM
I have a radgrid. In this RadGrid I have a DropDown list that contains value 0 to number of rows in grid-1. I want to move rows up and down on basis of dropdown selected index value for that grid. If in dropdown selected index is 2(Value is 1) then this rows should display at  osecond row in Rad grid.

Please suggest.

Thanks.

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 21 May 2012, 05:47 AM
Hi Sandeep,

I suppose you are placing the DropDownList in the GridTemplateColumn. Please take a look into the following code snippet.

ASPX:
<telerik:GridTemplateColumn>
    <ItemTemplate>
        <asp:DropDownList ID="DropDownList1" runat="server"  AutoPostBack="true" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Text="0" Value="0"></asp:ListItem>
            <asp:ListItem Text="1" Value="1"></asp:ListItem>
            <asp:ListItem Text="2" Value="2"></asp:ListItem>
            <asp:ListItem Text="3" Value="4"></asp:ListItem>
        </asp:DropDownList>
    </ItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
   DropDownList DropDownList1 = (DropDownList)sender;
   string selectedIndex = DropDownList1.SelectedItem.ToString();
   foreach (GridDataItem ditem in RadGrid1.Items)
   {
     string RowIndex = ditem.ItemIndex.ToString();
     if (RowIndex == selectedIndex)
     {
       ditem.Selected = true;
     }
   }
}

Please provide the code if it doesn't help.

Thanks,
Shinu.
0
Sandeep
Top achievements
Rank 1
answered on 21 May 2012, 02:30 PM
Thanks Sinhu.

Your code gives me idea about how can implement. But I want just move whole row according to selected digit from the dropdown list corresponding that rad grid row.

Suppose A grid contains 6 rows so each row dropdown list has value 1 to 6. when I select  3 from dropdown in grid rows number 5 then this row should move on number 3 position in grid.

Please suggest.

Thanks.
0
Shinu
Top achievements
Rank 2
answered on 22 May 2012, 05:25 AM
Hi Sandeep,

I guess you want to move the row according to the selected digit from the dropdown list corresponding the Radgrid's previously selected row.

C#:
static int count = 0;
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
   DropDownList DropDownList1 = (DropDownList)sender;
   int selectedIndex = DropDownList1.SelectedValue;
   count = count + selectedIndex;
   foreach (GridDataItem ditem in RadGrid1.Items)
   {
     int RowIndex = ditem.ItemIndex;
     if (RowIndex == count)
     {
       ditem.Selected = true;
     }
   }
}

Please elaborate your scenario if it doesn't help.

Thanks,
Shinu.
0
Sandeep
Top achievements
Rank 1
answered on 22 May 2012, 05:40 AM
Thanks Sinhu.

My Scenario is :

There is a RADGrid. in this grid each rows first column has a dropdown list. this dropdown populate value from 1 to total no rows in grid .


Suppose I am on row number 3 . From row number 3 dropdown list i selected value 5 from this dropdown list.when i selected 5 from this dropdown list then it swap with row number 5 means now row number 3 moved on row number 5 position in grid.

I hope It make a scene.

Please suggest.

Thanks.
0
Shinu
Top achievements
Rank 2
answered on 23 May 2012, 11:46 AM
Hi Sandeep,

I apologize for misleading you.Please take a look into the following code snippet i tried for swapping the current row with the dropdownlist selected indexed row.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView DataKeyNames="OrderID">   
        <Columns>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID"></telerik:GridBoundColumn>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <asp:DropDownList ID="ddl" runat="server" AutoPostBack="true" onselectedindexchanged="ddl_SelectedIndexChanged">
                            <asp:ListItem Text="select" ></asp:ListItem>
                            <asp:ListItem Text="0" Value="0"></asp:ListItem>
                            <asp:ListItem Text="1" Value="1"></asp:ListItem>
                            <asp:ListItem Text="2" Value="2"></asp:ListItem>
                            <asp:ListItem Text="3" Value="3"></asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
        </Columns>           
    </MasterTableView>
</telerik:RadGrid>

C#:
public DataTable DataSource
{
    get
    {
        object res = this.Session["_ds"];
        if (res == null)
        {
            string selectQuery1 = "select top 10 OrderID,CustomerID from Orders";
            SqlDataAdapter adapter1 = new SqlDataAdapter(selectQuery1, conn);
            DataTable dt1 = new DataTable();
            conn.Open();
            adapter1.Fill(dt1);
            conn.Close();
            RadGrid1.DataSource = dt1;
            this.Session["_ds"] = dt1;
        }
        return (DataTable)this.Session["_ds"];
    }
}
protected void  RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = this.DataSource;
}
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = (DropDownList)sender;
    GridDataItem item = (GridDataItem)ddl.NamingContainer;
    int index=item.ItemIndex;
    int selected = Convert.ToInt32(((System.Web.UI.WebControls.ListControl)(sender)).SelectedValue);
    GridItem movedItem = ((RadGrid1.MasterTableView.Controls[0] as Table).Rows[index] as GridItem);
    GridItem beforeItem = ((RadGrid1.MasterTableView.Controls[0] as Table).Rows[selected] as GridItem);
    object key1 = RadGrid1.MasterTableView.DataKeyValues[index]["OrderID"];
    object key2 = RadGrid1.MasterTableView.DataKeyValues[selected]["OrderID"];
    DataRow row1 = this.DataSource.Select(String.Format("OrderID = '{0}'", key1.ToString()))[0];
    DataRow row2 = this.DataSource.Select(String.Format("OrderID = '{0}'", key2.ToString()))[0];
    row1.BeginEdit();
    row2.BeginEdit();
    row2["OrderID"] = key1;
    row1["OrderID"] = key2;
    row1.EndEdit();
    row2.EndEdit();
    row1.AcceptChanges();
    row2.AcceptChanges();
    RadGrid1.Rebind();
}

Thanks,
Shinu.
Tags
Grid
Asked by
Sandeep
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Sandeep
Top achievements
Rank 1
Share this question
or