Ver Mensaje Individual
  #1 (permalink)  
Antiguo 18/11/2010, 04:50
dikcc
 
Fecha de Ingreso: junio-2010
Mensajes: 134
Antigüedad: 13 años, 10 meses
Puntos: 0
Insertar button en gridview creado dinamicamente

Quiero crear un GridView el cual genera las columnas al pulsar un boton. Cada vez que se pulsa un boton genera una columna donde hay un DropDownList, un TextBox y un Button, todos controles generados dinámicamente. Lo que quiero saber es como puedo asignar el evento Click a los botones generador dinamicamente.

Pongo el código que tengo hasta ahora, el cual genera el GridView.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["cont"] = 1;
ManagerListViewLiquidaciones mL = new ManagerListViewLiquidaciones();
DataSet ds = mL.ManagerListViewRellenarListView();
this.ListBox1.DataSource = ds.Tables[0];
this.ListBox1.DataTextField = "FICHERO";
this.ListBox1.DataValueField = "FICHERO";
this.ListBox1.DataBind();

DataSet ds2 = mL.ManagerListViewRellenarLiqFil(9, 2010);
this.ListBox2.DataSource = ds2.Tables[0];
this.ListBox2.DataTextField = "TipoLiqui";
this.ListBox2.DataValueField = "TipoLiqui";
this.ListBox2.DataBind();
Session["Nombre"] = 1;
DataTable dt = new DataTable();
Session["tabla"] = dt;
ArrayList l = new ArrayList();
Session["array"] = l;
}

}

protected void Button1_Click(object sender, EventArgs e)
{
//Preparo objetos a insertar
ArrayList l = (ArrayList)Session["array"];

ManagerListViewLiquidaciones mL = new ManagerListViewLiquidaciones();

DropDownList ddl = new DropDownList();
TextBox tBox = new TextBox();
Button btn = new Button();

DataSet ds2 = mL.ManagerListViewRellenarLiqFil(9, 2010);
ddl.ID = "ddlGridView" + Convert.ToString(Session["Nombre"]);
ddl.DataSource = ds2.Tables[0];
ddl.DataTextField = "TipoLiqui";
ddl.DataValueField = "TipoLiqui";
ddl.DataBind();

DataSet ds = mL.ManagerListViewRellenarListView();
tBox.ID = "txtGridView" + Convert.ToString(Session["Nombre"]);
tBox.Text = ds.Tables[0].Rows[0]["FICHERO"].ToString();

btn.Text = "Eliminar";
btn.ID = "btnEliminar" + Convert.ToString(Session["Nombre"]);
btn.CommandName = "eliminar";

btn.Command += new CommandEventHandler(this.Button2_Command);
btn.Click += new EventHandler(Button2_Click);

l.Add(ddl);
l.Add(tBox);
l.Add(btn);

this.Controls.Add(ddl);
this.Controls.Add(tBox);
this.Controls.Add(btn);

//Preparo DataTable
DataTable dt = (DataTable)Session["tabla"];
DataColumn idColumn = new DataColumn();
idColumn.ColumnName = Convert.ToString(Session["Nombre"]);
dt.Columns.Add(idColumn);

//Elijo si es la primera vez o las siguientes
if (Convert.ToString(Session["Nombre"]) == "1")
{
DataRow dr = dt.NewRow();

dt.Rows.Add(dr);

dr = dt.NewRow();

dt.Rows.Add(dr);

dr = dt.NewRow();

dt.Rows.Add(dr);

this.GridView1.DataSource = dt;
this.GridView1.DataBind();

this.GridView1.Rows[0].Cells[0].Controls.Add((DropDownList)l[0]);

//this.GridView1.Rows[0].Cells.Add(new TableCell());

//this.GridView1.Rows[0].Cells[1].Visible = false;

this.GridView1.Rows[1].Cells[0].Controls.Add((TextBox)l[1]);

//this.GridView1.Rows[1].Cells.Add(new TableCell());

//this.GridView1.Rows[1].Cells[1].Visible = false;

this.GridView1.Rows[2].Cells[0].Controls.Add((Button)l[2]);

Session["tabla"] = dt;

Session["Nombre"] = Convert.ToInt32(Session["Nombre"]) + 1;

Session["array"] = l;
}
else
{
this.GridView1.DataSource = dt;
this.GridView1.DataBind();

int cont = 0;

for (int i = 0; i < Convert.ToInt32(Session["Nombre"]); i++)
{
this.GridView1.Rows[0].Cells[i].Controls.Add((DropDownList)l[cont]);

this.GridView1.Rows[0].Cells[i].Visible = true;

//this.GridView1.Rows[0].Cells.Add(new TableCell());

//this.GridView1.Rows[0].Cells[i+1].Visible = false;

cont++;

this.GridView1.Rows[1].Cells[i].Controls.Add((TextBox)l[cont]);

this.GridView1.Rows[1].Cells[i].Visible = true;

//this.GridView1.Rows[1].Cells.Add(new TableCell());

//this.GridView1.Rows[1].Cells[i+1].Visible = false;

cont++;

this.GridView1.Rows[2].Cells[i].Controls.Add((Button)l[cont]);

this.GridView1.Rows[2].Cells[i].Visible = true;

cont++;
}

Session["tabla"] = dt;

Session["Nombre"] = Convert.ToInt32(Session["Nombre"]) + 1;

Session["array"] = l;
}
}

protected void Button2_Click(object sender, EventArgs e)
{
(sender as Button).Text = "You just clicked me!";

}

protected void Button2_Command(object sender, CommandEventArgs e)
{
Button bt = (Button)sender;

}
}

A ver si alguien me puede ayudar.
Gracias.

Última edición por dikcc; 18/11/2010 a las 04:56