can we use both custom button and inbuilt button in datagridview

Posted by Srikanth Mattihalli on Stack Overflow See other posts from Stack Overflow or by Srikanth Mattihalli
Published on 2010-04-29T09:22:12Z Indexed on 2010/04/29 9:27 UTC
Read the original article Hit count: 317

Filed under:
|

HI all,

I am using Datagridview in asp.net. I have used custom buttons of up and down in the datagridview along with edit,delete and paging options.

alt text

I am handling the up down buttons by raising events in rowcommand and the code is as below


string command = e.CommandName; Response.Write(e.CommandArgument.ToString()); int index = Convert.ToInt32(e.CommandArgument.ToString()); int count = GridView1.Rows.Count; int keyValue = Convert.ToInt32(GridView1.Rows[index].Cells1.Text); string value = GridView1.Rows[index].Cells[4].Text;

    SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString);
    SqlCommand cmd = new SqlCommand();


    if (command == "up")
    {
        if (index > 0)
        {
            index = index - 1;
            int keyValue1 = Convert.ToInt32(GridView1.Rows[index].Cells[1].Text);
            string value1 = GridView1.Rows[index].Cells[4].Text;
            cmd.Connection = conn;
            cmd.CommandText = "UPDATE [category] SET [order_id] = '" + value + "' WHERE [category_id]=" + keyValue1 + ";UPDATE [category] SET [order_id] = '" + value1 + "' WHERE [category_id]=" + keyValue + ";";

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

        }
    }
    else if (command == "down")
    {
        if (index < count - 1)
        {
            index = index + 1;
            int keyValue1 = Convert.ToInt32(GridView1.Rows[index].Cells[1].Text);
            string value1 = GridView1.Rows[index].Cells[4].Text;
            cmd.Connection = conn;
            cmd.CommandText = "UPDATE [category] SET [order_id] = '" + value + "' WHERE [category_id]=" + keyValue1 + ";UPDATE [category] SET [order_id] = '" + value1 + "' WHERE [category_id]=" + keyValue + ";";

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();               
        }
    }

    Response.Redirect("Default.aspx");

Designer file


" DeleteCommand="DELETE FROM [category] WHERE [category_id] = @category_id" InsertCommand="INSERT INTO [category] ([categoryname], [navigation_url], [order_id]) VALUES (@categoryname, @navigation_url, @order_id)" SelectCommand="SELECT * FROM [category] order by order_id" UpdateCommand="UPDATE [category] SET [categoryname] = @categoryname, [navigation_url] = @navigation_url, [order_id] = @order_id WHERE [category_id] = @category_id">

After this my edit,delete and paging is not working bcoz of event conflicts. Can anyone plz help me on this, so that i will be able to use both custom buttons(up and down) and edit,delete and paging features.

© Stack Overflow or respective owner

Related posts about c#

Related posts about datagridview