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

[Solved] Custom sort column

2 Answers 209 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ziga HABJAN
Top achievements
Rank 1
Ziga HABJAN asked on 21 May 2008, 09:36 AM
Hi!

Well so far i only used RADGrid to edit simple tables (auto generated radgrid). But now i got a table of links that has a fileld "sort" of type "int" in it which tells the order of how i want to display this links on my frontend.

So instead of having a databound column to that field (which would make user type the sort number to each record), i created a templated column with a "move up" and "move down" linkbuttons. I set the property "commandName" to "moveUp", "moveDown" and hooked onto "OnItemCommand" event of RadGrid

Now my question is, how can ni get reference on the grid row (to get id-key of my tabel) on which the linkbutton was clicked?

re,
Žiga

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 21 May 2008, 12:21 PM
Hi,

Set the required id-key as the DataKeyNames in the aspx and try the following code snippet to access the id-key in the ItemCommand event.

ASPX:
                  <MasterTableView DataKeyNames="ID"    DataSourceID="SqlDataSource1"
          
                        <Columns> 
                       
                        <telerik:GridTemplateColumn UniqueName="TempCol" > 
                         <ItemTemplate> 
                             <asp:LinkButton ID="LinkButton1" Text="moveUp"  runat="server" CommandName="moveUp" ></asp:LinkButton> 
                         </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                    </Columns> 


CS:
 protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        GridDataItem item=(GridDataItem)e.Item; 
        if (e.CommandName == "moveUp") 
        { 
            string strKey = item.GetDataKeyValue("ID").ToString(); 
        } 
 
    } 

Thanks
Shinu.
0
Ziga HABJAN
Top achievements
Rank 1
answered on 22 May 2008, 08:24 AM
Thanks alot :)


Here is the row moving procedure:

    private void SwapSortField(DataTable dt) {  
        var sqlSwap = "update footerLinks set sort=@sort1 where id=@id1; update footerLinks set sort=@sort2 where id=@id2;";  
        using (var con = CmsShared.GetSqlConnection()) {  
            using (var cmd = new SqlCommand(sqlSwap, con)) {  
                con.Open();  
                cmd.Parameters.AddWithValue("@sort1", dt.Rows[1]["sort"]);  
                cmd.Parameters.AddWithValue("@id1", dt.Rows[0]["id"]);  
                cmd.Parameters.AddWithValue("@sort2", dt.Rows[0]["sort"]);  
                cmd.Parameters.AddWithValue("@id2", dt.Rows[1]["id"]);  
                  
                cmd.ExecuteNonQuery();  
            }  
        }  
    }  
 
    protected void RadGrid1_OnItemCommand(object source, GridCommandEventArgs e) {  
            if (e.CommandName == "moveUp" || e.CommandName == "moveDown") {  
            var item=(GridDataItem)e.Item;   
            var sqlstring.Format("select top 2 * from footerLinks where sort {0}= (select sort from footerLinks where id=@id) order by sort {1}",  
                                                            e.CommandName == "moveUp" ? "<" : ">",   
                                                            e.CommandName == "moveUp" ? "desc" : "asc");  
            using (var con = CmsShared.GetSqlConnection()) {  
                using (var ada = new SqlDataAdapter(sql, con)) {  
                    ada.SelectCommand.Parameters.AddWithValue("@id", item.GetDataKeyValue("id"));  
                    using (var dt = new DataTable()) {  
                        ada.Fill(dt);  
                        if (dt.Rows.Count == 2) {  
                            SwapSortField(dt);  
                            RadGrid1.Rebind();  
                        }  
                    }  
                }  
            }  
        }  
    } 

For it to work, custom sorting on grid must be turned off and the data must always be sorted by "sort solumn".  I also added update query on my InsertCommand of SqlDataSource:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DobraPraksa %>" DeleteCommand="DELETE FROM [FooterLinks] WHERE [id] = @id" 
    InsertCommand="INSERT INTO [FooterLinks] ([title], [link], [sort]) VALUES (@title, @link, @sort); update footerLinks set sort=(select max(sort) + 1 from footerLinks) where id=(select @@identity);" 
    SelectCommand="SELECT [id], [title], [link], [sort] FROM [FooterLinks] order by sort" UpdateCommand="UPDATE [FooterLinks] SET [title] = @title, [link] = @link, [sort] = @sort WHERE [id] = @id"

So now, after inserting new record its sort value is updated to max(sort)+1 - placing it on bottom

re,
Žiga
Tags
Grid
Asked by
Ziga HABJAN
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Ziga HABJAN
Top achievements
Rank 1
Share this question
or