Fórum Como trabalhar com Campos DropDownList e CheckBox dentro de uma GridView Manual ? #1266
09/12/2008
0
<asp:Content ID="Content1" ContentPlaceHolderID="cpContent" Runat="Server">
<div id="content">
<h3>
Ação
</h3>
<asp:UpdatePanel id="UpdatePanel1" runat="server">
<contenttemplate>
<DIV class="box"><DIV class="top"><IMG class="right" alt="" src="images/box_top_right.gif" /> <IMG class="left" alt="" src="images/box_top_left.gif" /></DIV><DIV class="content"><SPAN class="row"><SPAN class="preField">Sistema:</SPAN> <SPAN class="field"><asp:DropDownList id="ddlSistema" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSistema_SelectedIndexChanged"></asp:DropDownList> </SPAN></SPAN><SPAN class="row"><SPAN class="preField">Módulo:</SPAN> <SPAN class="field"><asp:DropDownList id="ddlModulo" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlModulo_SelectedIndexChanged"></asp:DropDownList> </SPAN></SPAN><SPAN class="row"><SPAN class="preField">Função:</SPAN> <SPAN class="field"><asp:DropDownList id="ddlFuncao" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlFuncao_SelectedIndexChanged"></asp:DropDownList> </SPAN></SPAN><SPAN class="row"><SPAN class="preField">SubFunção:</SPAN> <SPAN class="field"><asp:DropDownList id="ddlSubFuncao" runat="server"></asp:DropDownList> </SPAN></SPAN><SPAN class="row"><SPAN class="preField">Ação:</SPAN> <SPAN class="field"><asp:TextBox id="tbAção" runat="server"></asp:TextBox> </SPAN></SPAN><SPAN class="row"><SPAN class="preField"></SPAN><asp:Button id="btIncluir" onclick="Incluir" runat="server" Text="Incluir" CssClass="button"></asp:Button> </SPAN><BR /></DIV><DIV class="bottom"><IMG class="right" alt="" src="images/box_bottom_right.gif" /> <IMG class="left" alt="" src="images/box_bottom_left.gif" /></DIV></DIV><asp:GridView id="gvAcao" runat="server" CssClass="grid" OnRowUpdating="Updating" OnRowEditing="Editing" OnRowDeleting="ExcluirDeleting" OnRowCommand="ExcluirCommand" DataKeyNames="CODIGO" Width="100%" BorderWidth="0px" EmptyDataText="Não há registros!" AutoGenerateColumns="false" AllowSorting="true" AllowPaging="true" OnPageIndexChanging="gvAcao_PageIndexChanging" OnRowCancelingEdit="CancelarEdit" OnRowDataBound="gvAcao_RowDataBound">
<RowStyle CssClass="gridLinhaPar"></RowStyle>
<Columns>
<asp:TemplateField HeaderText="Codigo" SortExpression="codigo"><EditItemTemplate>
<asp:Label id="lbGridCodigo1" runat="server" Text=<%# Bind("CODIGO") %> __designer:wfdid="w7"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label id="lbGridCodigo2" runat="server" Text=<%# Bind("CODIGO") %> __designer:wfdid="w1"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sistema" SortExpression="sistema"><EditItemTemplate>
<asp:DropDownList id="ddlGridSistema" runat="server" __designer:wfdid="w8"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbGridSistema" runat="server" Text=<%# Bind("SISTEMA") %> />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Modulo" SortExpression="modulo"><EditItemTemplate>
<asp:DropDownList id="ddlGridModulo" runat="server" __designer:wfdid="w5"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbGridModulo" runat="server" Text=<%# Bind("MODULO") %> />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Função" SortExpression="funcao"><EditItemTemplate>
<asp:DropDownList id="ddlGridFuncao" runat="server" __designer:wfdid="w4"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbGridFuncao" runat="server" Text=<%# Bind("FUNCAO") %> />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SubFunção" SortExpression="subfuncao"><EditItemTemplate>
<asp:DropDownList id="ddlGridSubFuncao" runat="server" __designer:wfdid="w3"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbGridSubFuncao" runat="server" Text=<%# Bind("SUB") %> />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Acão" SortExpression="acao"><EditItemTemplate>
<asp:TextBox id="tbGridAcao" runat="server" Text=<%# Bind("ACAO") %> __designer:wfdid="w2"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbGridAcao" runat="server" Text=<%# Bind("ACAO") %> />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False"><EditItemTemplate>
<asp:ImageButton ID="imgConfirm" runat="server" CausesValidation="true" CommandName="Update"
ImageUrl="~/images/icon_save.gif" />
<asp:ImageButton ID="imgCancel" runat="server" CausesValidation ="false" CommandName="Cancel"
ImageUrl="~/images/icon_exit.gif" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" CausesValidation="true" CommandName="Edit"
ImageUrl="~/images/icon_edit.gif" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False"><ItemTemplate>
<asp:ImageButton ID="imgDelete" runat="server" CommandArgument= <%# Eval("CODIGO") %> CausesValidation="false" CommandName="Delete"
ImageUrl="~/images/icon_trash.gif" />
</ItemTemplate>
</asp:TemplateField>
</Columns> <HeaderStyle CssClass="gridCabecalho"></HeaderStyle> <AlternatingRowStyle CssClass="gridLinhaImpar"></AlternatingRowStyle>
</asp:GridView> <SPAN class="controls"><asp:Button id="btSair" runat="server" Text="Sair" CssClass="button"></asp:Button> </SPAN>
</contenttemplate>
</asp:UpdatePanel>
</div>
</asp:Content> .cs using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Running.Data; public partial class Acao : System.Web.UI.Page
{
// String de Conexao //
string strConexao = ConfigurationManager.ConnectionStrings["rhdConnectionString"].ToString();
//-------------------// // Page Load //
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ExibirDados();
CarregarDDLSistema();
}
}
//----------// // Chamadas para as Classes de Preenchimento das DropDownLists //
protected void ddlSistema_SelectedIndexChanged(object sender, EventArgs e)
{
CarregarDDLModulo();
}
protected void ddlModulo_SelectedIndexChanged(object sender, EventArgs e)
{
CarregarDDLFuncao();
}
protected void ddlFuncao_SelectedIndexChanged(object sender, EventArgs e)
{
CarregarDDLSubFuncao();
}
//-----------------------------------------------------------// // Classes de Preenchimento das DropDownLists //
private void CarregarDDLSistema()
{
string SQL = "Select cd_sistema, ds_sistema from tb_sistema";
DataAccess da = new DataAccess();
da.OpenConection(); DataTable dt = da.ExecuteReader(new SqlCommand(SQL));
ddlSistema.DataSource = dt;
ddlSistema.DataValueField = "cd_sistema";
ddlSistema.DataTextField = "ds_sistema";
ddlSistema.DataBind(); ddlSistema.Items.Insert(0, new ListItem("Selecione", "0"));
}
private void CarregarDDLModulo()
{
string SQL = "Select cd_modulo, ds_modulo from tb_sistemamodulo where cd_sistema = " + ddlSistema.SelectedValue.ToString();
DataAccess da = new DataAccess();
da.OpenConection(); DataTable dt = da.ExecuteReader(new SqlCommand(SQL));
ddlModulo.DataSource = dt;
ddlModulo.DataValueField = "cd_modulo";
ddlModulo.DataTextField = "ds_modulo";
ddlModulo.DataBind(); ddlModulo.Items.Insert(0, new ListItem("Selecione", "0"));
}
private void CarregarDDLFuncao()
{
string SQL = "Select cd_sistemafuncao, ds_funcao from tb_sistemafuncao where cd_modulo = " + ddlModulo.SelectedValue.ToString();
DataAccess da = new DataAccess();
da.OpenConection(); DataTable dt = da.ExecuteReader(new SqlCommand(SQL));
ddlFuncao.DataSource = dt;
ddlFuncao.DataValueField = "cd_sistemafuncao";
ddlFuncao.DataTextField = "ds_funcao";
ddlFuncao.DataBind(); ddlFuncao.Items.Insert(0, new ListItem("Selecione", "0"));
}
private void CarregarDDLSubFuncao()
{
string SQL = "Select cd_sistemasubfuncao, ds_sistemasubfuncao from tb_sistemasubfuncao where cd_sistemafuncao = " + ddlFuncao.SelectedValue.ToString();
DataAccess da = new DataAccess();
da.OpenConection(); DataTable dt = da.ExecuteReader(new SqlCommand(SQL));
ddlSubFuncao.DataSource = dt;
ddlSubFuncao.DataValueField = "cd_sistemasubfuncao";
ddlSubFuncao.DataTextField = "ds_sistemasubfuncao";
ddlSubFuncao.DataBind(); ddlSubFuncao.Items.Insert(0, new ListItem("Selecione", "0"));
}
//--------------------------------------------//
// Classe para Carregar a GridView da Página //
protected void ExibirDados()
{
string SQL = "Select cd_sistemaacao CODIGO, ds_sistema SISTEMA, ds_modulo MODULO, " +
"ds_funcao FUNCAO, ds_sistemasubfuncao SUB, ds_acao ACAO from tb_sistemaacao " +
"INNER JOIN tb_sistema on tb_sistema.cd_sistema = tb_sistemaacao.cd_sistema " +
"INNER JOIN tb_sistemamodulo on tb_sistemamodulo.cd_modulo = tb_sistemaacao.cd_modulo " +
"INNER JOIN tb_sistemafuncao on tb_sistemafuncao.cd_sistemafuncao = tb_sistemaacao.cd_sistemafuncao " +
"INNER JOIN tb_sistemasubfuncao on tb_sistemasubfuncao.cd_sistemasubfuncao = tb_sistemaacao.cd_sistemasubfuncao " +
"ORDER BY ds_sistema, ds_modulo, ds_funcao, ds_sistemasubfuncao"; DataAccess da = new DataAccess(); da.OpenConection(); DataTable dt = da.ExecuteReader(new SqlCommand(SQL)); gvAcao.DataSource = dt;
gvAcao.DataBind(); da.CloseConection();
}
//------------------------------------------// // Função de Incluir //
protected void Incluir(object sender, EventArgs e)
{
DataSet ds = new DataSet();
string sSQL = "Insert into tb_sistemaacao (ds_acao,cd_sistema,cd_modulo,cd_sistemafuncao, cd_sistemasubfuncao)";
sSQL += " values ( " + tbAção.Text.ToString() + ", " + ddlSistema.SelectedValue + ", " + ddlModulo.SelectedValue + "," + ddlFuncao.SelectedValue + "," + ddlSubFuncao.SelectedValue + ")"; DataAccess da = new DataAccess(); da.OpenConection(); DataTable dt = da.ExecuteReader(new SqlCommand(sSQL)); ExibirDados(); da.CloseConection();
}
//------------------// // Funções para Excluir //
protected void ExcluirCommand(object sender, GridViewCommandEventArgs e)
{ if (e.CommandName == "Delete")
{
string Sql = "Delete tb_sistemaacao where cd_sistemaacao = " + e.CommandArgument;
DataAccess da = new DataAccess(); da.OpenConection(); int retorno = da.ExecuteNonQuery(Sql);
ExibirDados(); da.CloseConection();
} }
protected void ExcluirDeleting(object sender, GridViewDeleteEventArgs e)
{ }
//---------------------// //Função para Paginação //
protected void gvAcao_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
string SQL = "Select cd_sistemaacao CODIGO, ds_sistema SISTEMA, ds_modulo MODULO," +
" ds_funcao FUNCAO, ds_sistemasubfuncao SUB,ds_acao ACAO from tb_sistemaacao " +
"INNER JOIN tb_sistema on tb_sistema.cd_sistema = tb_sistemaacao.cd_sistema " +
"INNER JOIN tb_sistemamodulo on tb_sistemamodulo.cd_modulo = tb_sistemaacao.cd_modulo " +
"INNER JOIN tb_sistemafuncao on tb_sistemafuncao.cd_sistemafuncao = tb_sistemaacao.cd_sistemafuncao " +
"INNER JOIN tb_sistemasubfuncao on tb_sistemasubfuncao.cd_sistemasubfuncao = tb_sistemaacao.cd_sistemasubfuncao " +
"ORDER BY ds_sistema, ds_modulo, ds_funcao, ds_sistemasubfuncao";
DataAccess da = new DataAccess();
da.OpenConection();
DataTable dt = da.ExecuteReader(SQL); gvAcao.DataSource = dt;
gvAcao.PageIndex = e.NewPageIndex;
gvAcao.DataBind();
da.CloseConection();
}
//---------------------//
protected void Editing(object sender, GridViewEditEventArgs e)
{
gvAcao.EditIndex = e.NewEditIndex;
ExibirDados(); }
protected void Updating(object sender, GridViewUpdateEventArgs e)
{
/*GridViewRow row = gvAcao.Rows(e.RowIndex);
TextBox x = row.FindControl("tbGridAcao");
DropDownList y = row.FindControl("ddlGridSistema");
DropDownList z = row.FindControl("ddlGridModulo");
DropDownList w = row.FindControl("ddlGridFuncao");
DropDownList k = row.FindControl("ddlGridSubFuncao");
string sSQL= "Update tb_sistemaacao set ds_acao = "+x.Text+", cd_sistema*/ }
protected void CancelarEdit(object sender, GridViewCancelEditEventArgs e)
{
gvAcao.EditIndex = -1;
ExibirDados();
} protected void gvAcao_RowDataBound(object sender, GridViewRowEventArgs e)
{
// GridViewRow row = DropDownList ddlSistemas = new DropDownList();
ddlSistemas = (DropDownList)e.Row.FindControl("ddlGridSistema");
DropDownList ddlModulo = (DropDownList)e.Row.FindControl("ddlGridModulo");
DropDownList ddlFuncao = (DropDownList)e.Row.FindControl("ddlGridFuncao");
DropDownList ddlSubFuncao = (DropDownList)e.Row.FindControl("ddlGridSubFuncao"); string SQL = "Select cd_sistema, ds_sistema from tb_sistema"; DataAccess da = new DataAccess();
da.OpenConection(); DataTable dt = da.ExecuteReader(SQL);
ddlSistemas.DataSource = dt;
ddlSistema.DataValueField = "cd_sistema";
ddlSistema.DataTextField = "ds_sistema";
ddlSistema.DataBind();
}
}
Sergio Santos
Curtir tópico
+ 0Posts
10/12/2008
Guinther Pauli
Como você não está usando o SqlDataSource e programando tradicionalmente (com ADO.NET), eu não aconselho a utilização do SqlDataSource em runtime. Aconselho fazer todos os procedimentos de manutenção como no ASP.NET 1.1 clássico, ou seja, capturando os valores diretamente dos controles e gerando as instruções apropriadas com SqlCommand para atualização. Além disso, você pode trabalhar com o GridView sem necessariamente usar o SqlDataSource, bastando para isso usar um SqlDataReader ou um DataSet se for necessária a paginação.
Mais uma dica, eu aconselho fortemente o uso de StoredProcedures nesse caso, para processar as atualizações, pois aumentam drasticamente a performance da solução.
Att
Guinther
Gostei + 0
10/12/2008
Sergio Santos
Gostei + 0
11/12/2008
Sergio Santos
Gostei + 0
11/12/2008
[devmedia .net]
Gostei + 0
11/12/2008
Sergio Santos
Gostei + 0
11/12/2008
[devmedia .net]
Gostei + 0
11/12/2008
Sergio Santos
Gostei + 0
12/12/2008
[devmedia .net]
Gostei + 0
12/12/2008
[devmedia .net]
Gostei + 0
15/12/2008
[devmedia .net]
Gostei + 0
15/12/2008
Sergio Santos
[]s Sérgio
Gostei + 0
15/12/2008
[devmedia .net]
Gostei + 0
15/12/2008
Sergio Santos
Gostei + 0
15/12/2008
Sergio Santos
Gostei + 0
15/12/2008
[devmedia .net]
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)