How to bind text box values into Grid view control in asp.net


In this article I have explained How to bind text box values into grid view control in asp.net Just drag and drop two text box and one grid view control. With in button click event I have bind text box values into grid view control so drag and drop one button control also.

Just copy and paste below code and then run you web application. In run time you just enter "Emp id" and "emp name" in given text box field and then click submit button. these values will be saved to grid view control temporally. if you want to add more than one records then you can add another "empid" and "empname" once again in given text box control and then click submit button.I this example I'm not given any code to save records to Database table.



Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Dim DataRow As DataRow
Dim DsDataSet As DataSet
Public Shared Function GetFieldType(ByVal ftype As String) As System.Type
Select Case ftype
Case "int"
Return Type.GetType("System.Int32")
Case "varchar"
Return Type.GetType("System.String")
Case "datetime"
Return Type.GetType("System.DateTime")
Case "float"
Return Type.GetType("System.Double")
End Select
Return Type.GetType("System.Object")
End Function

Public Shared Function DefineDataset(ByVal Fields() As String, ByVal FieldTypes() As String, ByVal PrimaryKeys As String) As DataSet
Dim dstDataset As New DataSet
Dim dtTable As New DataTable
Dim icount As Integer
For icount = 0 To FieldTypes.Length - 1
dtTable.Columns.Add(New DataColumn(Fields(icount), GetFieldType(FieldTypes(icount))))
If Fields(icount) = PrimaryKeys Then
dtTable.Constraints.Add("c" & icount, dtTable.Columns(icount), True)
End If
Next
dstDataset.Tables.Add(dtTable)
Return (dstDataset)
End Function
Public Property GridBindDataSetViewstate() As DataSet
Get
If Not (ViewState("GridBindDataSetViewstate") Is Nothing) Then
DsDataSet = CType(ViewState("GridBindDataSetViewstate"), DataSet)
End If
If DsDataSet Is Nothing Then
DsDataSet = DefineDataset(New String() {"EmpID", "EmpName"}, New String() {"varchar", "varchar"}, "")
ViewState.Add("GridBindDataSetViewstate", DsDataSet)
End If
Return DsDataSet
End Get

Set(ByVal value As DataSet)
DsDataSet = value
If Not (ViewState("GridBindDataSetViewstate") Is Nothing) Then
ViewState("GridBindDataSetViewstate") = DsDataSet
Else
ViewState.Add("GridBindDataSetViewstate", DsDataSet)
End If
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim drwRow As DataRow = GridBindDataSetViewstate.Tables(0).NewRow()
drwRow("EmpID") = ""
drwRow("EmpName") = ""
GridBindDataSetViewstate.Tables(0).Rows.Add(drwRow)
grvEmpDetails.DataSource = GridBindDataSetViewstate
grvEmpDetails.DataBind()
End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSumbit.Click

Dim drwRow As DataRow = GridBindDataSetViewstate.Tables(0).NewRow()
drwRow("EmpID") = txtEmpID.Text
drwRow("EmpName") = txtEmpName.Text
GridBindDataSetViewstate.Tables(0).Rows.Add(drwRow)
grvEmpDetails.DataSource = GridBindDataSetViewstate
grvEmpDetails.DataBind()
End Sub



Protected Sub grvEmpDetails_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grvEmpDetails.Sorting
Dim dv As DataView = GridBindDataSetViewstate.Tables(0).DefaultView
dv.Sort = e.SortExpression
grvEmpDetails.DataSource = dv
grvEmpDetails.DataBind()
End Sub

Protected Sub grvEmpDetails_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grvEmpDetails.PageIndexChanging
grvEmpDetails.PageIndex = e.NewPageIndex
grvEmpDetails.DataSource = GridBindDataSetViewstate
grvEmpDetails.DataBind()
End Sub
End Class


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtEmpID" runat="server"></asp:TextBox>
<asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
<asp:Button ID="btnSumbit" runat="server" Text="Submit" />
<asp:GridView ID="grvEmpDetails" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>



Comments

Author: Umesh Bhosale02 Apr 2014 Member Level: Silver   Points : 2

Hello
You can also do it into rowdatabound event of grid view.
steps
1. First find out textbox control from
TextBox txtName=(TextBox)e.Row.FindControl("txtname")
2. use datakeys value to assign to textbox value


Thanks
Umesh



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: