Principales respuestas
pasar datos de textbox a gridview

Pregunta
-
Hola a todos
Soy nuevo en prog Asp Net estoy desarrollando una aplicacion en donde necesito pasar datos desde varios text box a un gridview sin conexion a datos y no encuentro ayuda de como hacerlo. por favor si alguen me puede dar una mano, utilizo lenguaje vb.Net
Muchas gracias
Respuestas
-
hola,
#Escrito por :cnmerc##Hola a todos##Soy nuevo en prog Asp Net estoy desarrollando una aplicacion#en donde necesito pasar datos desde varios text box a un gridview#sin conexion a datos y no encuentro ayuda de como hacerlo. por favor#si alguen me puede dar una mano, utilizo lenguaje vb.Net
puede hacer uso del DataTables con junto con DataRow te pongo un ejemplo 100%Funcional.
DataTable dt = new DataTable();DataRow Row1;DataRow Row2;dt.Columns.Add(new DataColumn("Nombre", System.Type.GetType("System.String")));dt.Columns.Add(new DataColumn("Apellido", System.Type.GetType("System.String")));
Row1 = dt.NewRow();Row1["Nombre"] = this.txtnombre.Text;Row1["Apellido"] = this.txtapellido.Text;dt.Rows.Add(Row1);
Row2 = dt.NewRow();Row2["Nombre"] = this.txtnombre.Text;Row2["Apellido"] = this.txtapellido.Text;dt.Rows.Add(Row2);
GridView1.DataSource = dt;GridView1.DataBind();
ASP.NET,ASP.NET MVC C#.NET- Propuesto como respuesta Enmanuel GrullardModerator domingo, 20 de febrero de 2011 14:03
- Marcado como respuesta Eduardo PorteschellerModerator martes, 22 de febrero de 2011 17:36
Todas las respuestas
-
hola,
#Escrito por :cnmerc##Hola a todos##Soy nuevo en prog Asp Net estoy desarrollando una aplicacion#en donde necesito pasar datos desde varios text box a un gridview#sin conexion a datos y no encuentro ayuda de como hacerlo. por favor#si alguen me puede dar una mano, utilizo lenguaje vb.Net
puede hacer uso del DataTables con junto con DataRow te pongo un ejemplo 100%Funcional.
DataTable dt = new DataTable();DataRow Row1;DataRow Row2;dt.Columns.Add(new DataColumn("Nombre", System.Type.GetType("System.String")));dt.Columns.Add(new DataColumn("Apellido", System.Type.GetType("System.String")));
Row1 = dt.NewRow();Row1["Nombre"] = this.txtnombre.Text;Row1["Apellido"] = this.txtapellido.Text;dt.Rows.Add(Row1);
Row2 = dt.NewRow();Row2["Nombre"] = this.txtnombre.Text;Row2["Apellido"] = this.txtapellido.Text;dt.Rows.Add(Row2);
GridView1.DataSource = dt;GridView1.DataBind();
ASP.NET,ASP.NET MVC C#.NET- Propuesto como respuesta Enmanuel GrullardModerator domingo, 20 de febrero de 2011 14:03
- Marcado como respuesta Eduardo PorteschellerModerator martes, 22 de febrero de 2011 17:36
-
hola
se habia realizado una consulta similar hace un timepo
http://social.msdn.microsoft.com/Forums/es/webdeves/thread/d8b8ec22-2dc9-41be-819b-5c685b3b2bf3
recuerda mantener los datos en Sesseion para conservarlos entre postback y poder agregar alli la info del textbox
resumen la info no la agregas directo al gridview sino a los datos que bidneas al mismo
saludos
Leandro Tuttini
Blog
Buenos Aires
Argentina -
C#
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; namespace WebApplication1._01_Foro { public partial class WebForm3 : System.Web.UI.Page { // create a new dataset private static System.Data.DataSet dataSet = new DataSet(); // Create a new DataTable. private static System.Data.DataTable table = new DataTable("ParentTable"); // Declare variables for DataColumn and DataRow objects. private static DataColumn column; private static DataRow row; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Create new DataColumn, set DataType, // ColumnName and add to DataTable. column = new DataColumn(); column.DataType = System.Type.GetType("System.Int32"); column.ColumnName = "id"; column.AutoIncrement = false; //column.Caption = "ID"; column.ReadOnly = false; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); // Create second column. column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "ParentItem"; column.AutoIncrement = false; //column.Caption = "ParentItem"; column.ReadOnly = false; column.Unique = false; // Add the column to the table. table.Columns.Add(column); // Make the ID column the primary key column. DataColumn[] PrimaryKeyColumns = new DataColumn[1]; PrimaryKeyColumns[0] = table.Columns["id"]; table.PrimaryKey = PrimaryKeyColumns; // Add the new DataTable to the DataSet. dataSet.Tables.Add(table); } } protected void Button1_Click(object sender, EventArgs e) { // add row to the table row = table.NewRow(); row["id"] = Convert.ToInt32(TextBox1.Text); row["ParentItem"] = TextBox2.Text; table.Rows.Add(row); GridView1.DataSource = dataSet.Tables[0]; GridView1.DataBind(); } } }
VB
Imports System.Collections.Generic Imports System.Linq Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Data Namespace WebApplication1._01_Foro Public Partial Class WebForm3 Inherits System.Web.UI.Page ' create a new dataset Private Shared dataSet As System.Data.DataSet = New DataSet() ' Create a new DataTable. Private Shared table As System.Data.DataTable = New DataTable("ParentTable") ' Declare variables for DataColumn and DataRow objects. Private Shared column As DataColumn Private Shared row As DataRow Protected Sub Page_Load(sender As Object, e As EventArgs) If Not IsPostBack Then ' Create new DataColumn, set DataType, ' ColumnName and add to DataTable. column = New DataColumn() column.DataType = System.Type.[GetType]("System.Int32") column.ColumnName = "id" column.AutoIncrement = False 'column.Caption = "ID"; column.[ReadOnly] = False column.Unique = False ' Add the Column to the DataColumnCollection. table.Columns.Add(column) ' Create second column. column = New DataColumn() column.DataType = System.Type.[GetType]("System.String") column.ColumnName = "ParentItem" column.AutoIncrement = False 'column.Caption = "ParentItem"; column.[ReadOnly] = False column.Unique = False ' Add the column to the table. table.Columns.Add(column) ' Make the ID column the primary key column. Dim PrimaryKeyColumns As DataColumn() = New DataColumn(0) {} PrimaryKeyColumns(0) = table.Columns("id") table.PrimaryKey = PrimaryKeyColumns ' Add the new DataTable to the DataSet. dataSet.Tables.Add(table) End If End Sub Protected Sub Button1_Click(sender As Object, e As EventArgs) ' add row to the table row = table.NewRow() row("id") = Convert.ToInt32(TextBox1.Text) row("ParentItem") = TextBox2.Text table.Rows.Add(row) GridView1.DataSource = dataSet.Tables(0) GridView1.DataBind() End Sub End Class End Namespace
Angel R. Jimenez G.
Software Development
Santo Domingo
Republica Dominicana- Editado Angel Jimenez domingo, 20 de febrero de 2011 17:29 Modificar
- Propuesto como respuesta pv_isc martes, 27 de marzo de 2012 18:40
-
-
Hola amigo, Quizá me puedas ayudar. Yo estoy haciendo un plan de pago de un préstamo en asp.net VB.
El asunto es el Siguiente:
Cuando doy clic en calcular Plan de Pago.
Recorre un ciclo For dependiendo el tiempo que deseen el préstamo. Por cada vuelta que da, almacena datos en 7 variables distintas y debería de insertarlos en el GridView pero me da error al momento de insertar uno de esos datos en cada celda de una fila. Yo se que no se pueden insertar directamente y que necesito usar DataTables, pero cuando no se como porque siempre me da error. Espero me puedan ayudar. de ante mano Gracias.!
-
Hola amigos, Quizá me puedas ayudar. Yo estoy haciendo un plan de pago de un préstamo en asp.net VB.
El asunto es el Siguiente:
Cuando doy clic en calcular Plan de Pago.
Recorre un ciclo For dependiendo el tiempo que deseen el préstamo. Por cada vuelta que da, almacena datos en 7 variables distintas y debería de insertarlos en el GridView pero me da error al momento de insertar uno de esos datos en cada celda de una fila. Yo se que no se pueden insertar directamente y que necesito usar DataTables, pero cuando no se como porque siempre me da error. Espero me puedan ayudar. de ante mano Gracias.!