我们可以在datagridview中使用自定义按钮和内置按钮

时间:2010-04-29 09:22:12

标签: c# datagridview

全部,

我在asp.net中使用Datagridview。我在datagridview中使用了向上和向下的自定义按钮以及编辑,删除和分页选项。

alt text http://i41.tinypic.com/90dbmx.jpg

我通过在rowcommand中引发事件来处理向上按钮,代码如下所示


    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].Cells[1].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");

设计师档案


<div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="category_id" DataSourceID="SqlDataSource1" 
        AllowPaging="True" onrowcommand="GridView1_RowCommand" 
        onselectedindexchanged="GridView1_SelectedIndexChanged" 
        AllowSorting="True">

        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="category_id" HeaderText="category_id" 
                InsertVisible="False" ReadOnly="True" SortExpression="category_id" />
            <asp:BoundField DataField="categoryname" HeaderText="categoryname" 
                SortExpression="categoryname" />
            <asp:BoundField DataField="navigation_url" HeaderText="navigation_url" 
                SortExpression="navigation_url" />
            <asp:BoundField DataField="order_id" HeaderText="order_id" 
                SortExpression="order_id" />
            <asp:ButtonField ButtonType="Image" CommandName="up" Text="up" 
                ImageUrl="~/images/up.png" />
            <asp:ButtonField ButtonType="Image" CommandName="down" 
                ImageUrl="~/images/down.png" Text="down" />
                <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="btnSubmit"/>
                </ItemTemplate>
                </asp:TemplateField>
            <asp:ButtonField CommandName="edit" Text="Edit" />
        </Columns>

    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:pp_cmsConnectionString %>" 
        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">
        <DeleteParameters>
            <asp:Parameter Name="category_id" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="categoryname" Type="String" />
            <asp:Parameter Name="navigation_url" Type="String" />
            <asp:Parameter Name="order_id" Type="Decimal" />
            <asp:Parameter Name="category_id" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="categoryname" Type="String" />
            <asp:Parameter Name="navigation_url" Type="String" />
            <asp:Parameter Name="order_id" Type="Decimal" />
        </InsertParameters>
    </asp:SqlDataSource>

</div>

在此之后我的编辑,删除和分页不起作用事件冲突的bcoz。任何人都可以帮助我这个,这样我就可以使用自定义按钮(向上和向下)以及编辑,删除和分页功能。

1 个答案:

答案 0 :(得分:1)

我认为你的问题是最后的重定向 如果用户单击标准编辑/删除按钮,您的OnRowCommand方法仍将首先执行,这可能就是他们停止工作的原因。 目前尚不清楚为什么需要重定向,但是你应该确保只在执行一个自定义命令时调用它。