Here the document explains that how to create the simple GridView and binding it with the sample data table in the application itself without using the database.
First, Create a sample user form and save the details those are entered in the form and the saved details should bind with the GridView. The source code as follows,
And coming to the Coding part, The Namespaces we need to add as,
- using System;
- using System.Collections.Generic;
- using System.Linq;
- Using System.Web;
- Using System.Web.UI;
- Using System.Web.UI.WebControls;
- Using System.Data;
And when we click on the “SAVE YOUR BUTTON” it will automatically generate an event, in that event we need to write the code using the Data Table concept as follows,
- Protectedvoid Button1_Click(object sender, EventArgs e)
- {
- DataTabledt = newDataTable();
- dt.Columns.Add("Username", typeof(string));
- dt.Columns.Add("Password", typeof(string));
- dt.Columns.Add("Email ID", typeof(string));
- dt.Columns.Add("Address", typeof(string));
- dt.Columns.Add("Contact", typeof(string));
- dt.Columns.Add("Gender", typeof(string));
- If(TextBox2.Text == TextBox3.Text)
- {
- dt.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox4.Text, TextBox5.Text, RadioButtonList1.SelectedItem.Text);
-
- GridView1.DataSource = dt;
- GridView1.DataBind();
- } else
- {
- Response.Write("<script>alert ('Password and ConformPassword are not matched') ;< /script>");
- }
-
- }
Then it will check the Password and Confirm Password, If both are same then it will save the data and display data in GridView. If those are not same it will display a pop-up message to the user.