sexta-feira, 6 de setembro de 2013

Movendo linhas (rows) no GridView com C#

Normalmente precisamos de interagir com a informação gerada no grid, mover algumas linhas para cima ou para baixo, para melhor visualizar ou até mesmo comparar informações, para isso utilize o exemplo abaixo.




Código HTML:
        <div>
        <asp:Label ID="Label1" runat="server" ForeColor="red" Text="Label"></asp:Label>
        <br />
        <br />
            <asp:GridView ID="Gridviewselectbus" runat="server" Height="87px" Width="650px"
                HorizontalAlign="Center" AutoGenerateColumns="False"
                OnRowCommand="Gridviewselectbus_RowCommand" CellPadding="4" ForeColor="#333333"
                GridLines="None">
                <RowStyle BorderColor="#999999" HorizontalAlign="Center" VerticalAlign="Middle"
                    Wrap="True" BackColor="#EFF3FB" />
                <EmptyDataRowStyle BorderColor="#999999" />
                <Columns>
                    <asp:BoundField DataField="Nome" HeaderText="Nome" SortExpression="Nome" />
                    <asp:BoundField DataField="Sobrenome" HeaderText="Sobrenome" SortExpression="Sobrenome" />
                    <asp:BoundField DataField="Cargo" HeaderText="Cargo" SortExpression="Cargo" />
                    <asp:BoundField DataField="Index"  HeaderText="Index" SortExpression="Index" />
                    <asp:TemplateField>
                        <HeaderStyle Width="3%" />
                        <ItemTemplate>
                            <asp:ImageButton ID="ibtnUp" runat="server" border="0" CommandArgument="<%# Eval("index")%>"
                                CommandName="Cima" Height="18px" ImageUrl="images/seta_cima.jpg" Width="18px" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderStyle Width="3%" />
                        <ItemTemplate>
                            <asp:ImageButton ID="ibtnDown" runat="server" border="0" CommandArgument="<%# Eval("index")%>"
                                CommandName="Baixo" Height="18px" ImageUrl="images/seta_baixo.gif" Width="18px" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
        </div>

Nosso Código C#:
    public DataTable dt = new DataTable();
    public DataTable dtnew = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindgrid(); // Bind Gridview (exemplo de dados)
        }

    }

    private void bindgrid()
    {
        dt.Columns.Add("Sobrenome");
        dt.Columns.Add("Nome");
        dt.Columns.Add("Cargo");
        dt.Columns.Add("Index");
        DataRow dr;
        dr = dt.NewRow();
        dr[0] = "Edgar";
        dr[1] = "Esteves";
        dr[2] = "Desenvolvedor";
        dr[3] = 0;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr[0] = "João";
        dr[1] = "Zinho";
        dr[2] = "Comediante";
        dr[3] = 1;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr[0] = "Roberto";
        dr[1] = "Carlos";
        dr[2] = "Musico ou Jogador";
        dr[3] = 2;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr[0] = "Maria";
        dr[1] = "Da Cruz";
        dr[2] = "Freira";
        dr[3] = 3;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr[0] = "Seu";
        dr[1] = "Cleiton";
        dr[2] = "Vendedor";
        dr[3] = 4;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr[0] = "Lula";
        dr[1] = "Lula rs..";
        dr[2] = "Presidente";
        dr[3] = 5;
        dt.Rows.Add(dr);
        dt.AcceptChanges();
        Gridviewselectbus.DataSource = dt;
        Gridviewselectbus.DataBind();
        Session["dt"] = dt;
    }

    protected void Gridviewselectbus_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Cima")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            if (index == 0)
            {
                Label1.Text = "Moveu para Cima";
                Label1.Visible = true;
                return;
            }
            dt = (DataTable)Session["dt"];
            int value = Convert.ToInt32(dt.Rows[index]["Index"].ToString());
            dt.Rows[index]["Index"] = Convert.ToInt32(index) - 1;
            dt.Rows[index - 1]["Index"] = value;// Convert.ToInt32(index);
            dt.DefaultView.Sort = "Index";
            dt.AcceptChanges();
            dtnew = dt.Copy();
            Gridviewselectbus.DataSource = dt;
            Gridviewselectbus.DataBind();
            dt.AcceptChanges();
            for (int i = 0; i <= Gridviewselectbus.Rows.Count - 1; i++)
            {
                dtnew.Rows[i]["Nome"] = Gridviewselectbus.Rows[i].Cells[0].Text;
                dtnew.Rows[i]["Cargo"] = Gridviewselectbus.Rows[i].Cells[1].Text;
                dtnew.Rows[i]["Sobrenome"] = Gridviewselectbus.Rows[i].Cells[2].Text;
                dtnew.Rows[i]["Index"] = Gridviewselectbus.Rows[i].Cells[3].Text;
            }
            Session["dt"] = dtnew;
            Label1.Text = string.Empty;
        }
        if (e.CommandName == "Baixon")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            dt = (DataTable)Session["dt"];
            if (Convert.ToInt16(index + 1) == dt.Rows.Count)
            {
                Label1.Text = "Moveu para Baixo";
                Label1.Visible = true;
                return;
            }
            int value = Convert.ToInt32(dt.Rows[index]["Index"].ToString());
            dt.Rows[index]["Index"] = Convert.ToInt32(dt.Rows[index]["Index"].ToString()) + 1;
            dt.Rows[index + 1]["Index"] = value;
            dt.AcceptChanges();
            dt.DefaultView.Sort = "Index";
            dt.AcceptChanges();
            dtnew = dt.Copy();
            Gridviewselectbus.DataSource = dt;
            Gridviewselectbus.DataBind();
            dt.AcceptChanges();
            for (int i = 0; i <= Gridviewselectbus.Rows.Count - 1; i++)
            {
                dtnew.Rows[i]["Nome"] = Gridviewselectbus.Rows[i].Cells[0].Text;
                dtnew.Rows[i]["Cargo"] = Gridviewselectbus.Rows[i].Cells[1].Text;
                dtnew.Rows[i]["Sobrenome"] = Gridviewselectbus.Rows[i].Cells[2].Text;
                dtnew.Rows[i]["Index"] = Gridviewselectbus.Rows[i].Cells[3].Text;
            }
            Session["dt"] = dtnew;
            Label1.Text = string.Empty;
        }
    }

Bons, estudos

0 comentários:

Postar um comentário