Category Archives: GridView Example

Insert Update Delete data in Gridview asp.net using SQLDataadapter method

In previous asp.net post we have discussed about insert update and delete data in gridview using dataset and table  adapter method.

In this asp.net post we will learn insert edit update and delete data in gridview control in asp.net using SQLConnection and SQLDataAdapter method. we can do the same thing by using other connection method like LINQ connection and SQLCommand and stored procedure method.

For understand edit update delete gridview example, First we insert records in to database and select records from database and bind it to gridview control, then update and delete record in gridview using sql dataadater method.

 Step for Insert Edit Update Delete GridView asp.net example

Step 1 – Create database table and create new asp.net web application.

Step 2 – Design asp.net web page for gridview example.

Step 3 – Add name space for sql connection DataTable object. 

Step 4 – using System.Data.SqlClient; using System.Data;

Step 5 – AutoGenerateDeleteButton=”True” and AutoGenerateEditButton=”True”

Step 6 –Declare SQLConnction and SQLDataAdapter object for connectivity.

Step 7 – Insert record to gridview and update, delete records in gridview.

For understand this asp.net gridview example we need to create sql database table.

Here, we have created UserMst table for doing this example.

This is the script for create sql table.

Now, after creating sql table design asp.net web page for gridview example.

 HTML Source code for asp.net web page

 

Insert Update Delete data in gridview control in asp.net c#

Insert Update Delete data in gridview control in asp.net c#

Now, finishing design make connection between sql database and asp.net web application. In this asp.net gridview example we use sql connection and sql dataadapter method for connectivity. In this method we write sql  query in our code behind page instead of writing sql stored procedure.

Now, set gridview property shows like below.

AutoGenerateDeleteButton=”True”
AutoGenerateEditButton=”True”
DataKeyNames=”ID”

Insert Update Delete data in gridview control in asp.net c#

Insert Update Delete data in gridview control in asp.net c#

Enable Gridview events for Edit update delete records in gridview.

onrowcancelingedit=”GridView1_RowCancelingEdit”
onrowdeleting=”GridView1_RowDeleting”
onrowediting=”GridView1_RowEditing”
onrowupdating=”GridView1_RowUpdating”

Insert Update Delete data using Gridview in asp.net C#

First add namespace for sql connectivity.

Here is C# sample code for insert update delete record using gridview in asp.net

 

 Gridview Example Result :

Insert Update Delete data in gridview control in asp.net c#

Insert Update Delete data in gridview control in asp.net c#

Insert Update Delete data in gridview control in asp.net c#

Insert Update Delete data in gridview control in asp.net c#

 

Insert Update Delete GridView in ASP.Net C# VB

In this asp.net post we will learn, how to insert update and delete records in gridview using asp.net C# and VB language.

To understand this example we must make connection between mssql server database and asp.net web application. There are many method to make connection between sql server and asp.net application. This gridview example we are using dataset and stored procedure method for connectivity. Next asp.net example we will make connection using SqlConnection and SqlDataadapter method and SqlCommmand and Stored Procedure method and Linq method.

In this asp.net gridview example first insert data in to database and then select data and  recorbind to gridview. After bind data to gridview we will edit and update records in gridview and simple delete records from gridview by clicking delete button.

Step for Insert Update Delete GridView asp.net example

Step 1 – Create database table and create asp.net web application.

Step 2 – Create Sql Stored Procedure for Select, Insert, Update and Delete data.

Step 3 – Design asp.net web page for gridview example.

Step 4 – Add new DataSet for sql connectivity, Bind all stored procedure to dataset.

Step 5 – AutoGenerateDeleteButton=”True” and AutoGenerateEditButton=”True”

Step 6 – Insert record to gridview and update, delete records in gridview.

For understand this asp.net gridview example we need to create database table.

Here, we have created UserMst table for doing this example. This is the script for create sql table.

 

create new table in sql server databse.

create new table in sql server databse.

Now, design asp.net web page for insert update delete gridview example.

Here is HTML code for asp.net web page.

 

 

Insert Update Delete data  in gridview using asp.net c# and vb.

Insert Update Delete data in gridview using asp.net c# and vb.

After design asp.net web page for gridview example, now make connectivity between asp.net application and database table.

We are using DataSet and Stored Procedure method for connectivity.

Set Gridview Property

AutoGenerateDeleteButton=”True”
AutoGenerateEditButton=”True”
DataKeyNames=”ID”

 

Enable Gridview events for Edit update delete records inn gridview.

onrowcancelingedit=”GridView1_RowCancelingEdit”
onrowdeleting=”GridView1_RowDeleting”
onrowediting=”GridView1_RowEditing”
onrowupdating=”GridView1_RowUpdating”

Insert Update Delete Records in Gridview using asp.net C#

Here is the c# code for simple insert update and delete operation in gridview control in asp.net.

 

 

Insert Update Delete Records in Gridview using asp.net VB

 

 

Asp.net gridview Insert Update Delete example output is :

Insert Update Delete data  in gridview using asp.net c# and vb.

Insert Update Delete data in gridview using asp.net c# and vb.

Insert Update Delete data  in gridview using asp.net c# and vb.

Insert Update Delete data in gridview using asp.net c# and vb.

 

Export GridView Data to Excel Sheet in ASP.Net C#

In this asp.net article i will explain you how to export gridview control data to ms Excel Sheet in asp.net with C#.

First Design asp.net form with Gridview contorl and one Button control.

Here, we need to bind data from server to Gridview control on page load event.

How to Export GridView Data to Excel Sheet in ASP.Net C#

How to Export GridView Data to Excel Sheet in ASP.Net C#

The HTML Design code is :

 <asp:GridView ID=”GridView1″ runat=”server” BackColor=”White”
BorderColor=”#CC9966″ BorderStyle=”None” BorderWidth=”1px” CellPadding=”4″>
<RowStyle BackColor=”White” ForeColor=”#330099″ />
<FooterStyle BackColor=”#FFFFCC” ForeColor=”#330099″ />
<PagerStyle BackColor=”#FFFFCC” ForeColor=”#330099″ HorizontalAlign=”Center” />
<SelectedRowStyle BackColor=”#FFCC66″ Font-Bold=”True” ForeColor=”#663399″ />
<HeaderStyle BackColor=”#990000″ Font-Bold=”True” ForeColor=”#FFFFCC” />
</asp:GridView>
<br />
<asp:Button ID=”Button1″ runat=”server” Font-Bold=”True” Height=”34px”
onclick=”Button1_Click” Text=”EXPORT TO EXCEL” />

After Design the asp.net webform as above bind the data to Gridview control in Page_load Event.

First Add name space for sqlconnection and StringWriter as below :

using System.Data.SqlClient;
using System.IO;

Now write below code on page_load event for BindGridview control.

  protected void Page_Load(object sender, EventArgs e)
{
SqlConnection SQLConn = new SqlConnection(“Data Source=’.\\SQLEXPRESS’;Integrated Security=’true’;Initial Catalog=’Example'”);
SqlDataAdapter SQLAdapter = new SqlDataAdapter(“Select * from UserMst”, SQLConn);
DataTable DT = new DataTable();
SQLAdapter.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
}

How to Export GridView Data to Excel Sheet in ASP.Net C#

How to Export GridView Data to Excel Sheet in ASP.Net C#

Now , write below code on Export to Excel Button control in asp.net code page:

protected void Button1_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.AddHeader(“content-disposition”,
“attachment;filename=Users.xls”);
Response.ContentType = “applicatio/excel”;
StringWriter sw = new StringWriter(); ;
HtmlTextWriter htm = new HtmlTextWriter(sw);
GridView1.RenderControl(htm);
Response.Write(sw.ToString());
Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{

}

How to Export GridView Data to Excel Sheet in ASP.Net C#

How to Export GridView Data to Excel Sheet in ASP.Net C#

How to Export GridView Data to Excel Sheet in ASP.Net C#

How to Export GridView Data to Excel Sheet in ASP.Net C#

How to Export GridView Data to Excel Sheet in ASP.Net C#

How to Export GridView Data to Excel Sheet in ASP.Net C#

 

I hope this Export GridView Data to Excel Sheet in ASP.Net will help you….

ADD manually Columns in GridView control in asp.net

we will learn here insert data to sql server and bind it to Gridview control in asp.net

first create database in sql server then create table in sql server.

The Databse name = Example and Table name = UserMst

we have here four columns ID, Name, Surname, City in Table.

Design web form in asp.net as follow:

Add manually create columns in gridview control

Add manually create columns in gridview control

 

Now, set AutoGenerateColumns = false for hide autogenerated columns in gridview.

Add manually create columns in gridview control

Add manually create columns in gridview control

After set AutogenerateColumns = false then add manually columns in gridview as follow:

Add manually create columns in gridview control

Add manually create columns in gridview control

 

we can add manually columns in gridview as follow:

Add manually create columns in gridview control

Add manually create columns in gridview control

we can also add manually columns by writting below code in HTML code of web page:

<asp:GridView ID=”GridView1″ runat=”server” Width=”247px”>
<Columns>
<asp:BoundField DataField=”Name” HeaderText=”Name” SortExpression=”Name” />
<asp:BoundField DataField=”Surname” HeaderText=”Surname”
SortExpression=”Surname” />
<asp:BoundField DataField=”city” HeaderText=”City” SortExpression=”city” />
</Columns>
<HeaderStyle BackColor=”#FF99FF” />
</asp:GridView>

Add manually create columns in gridview control

Add manually create columns in gridview control

 

write below code in Button control for add data to sql server and then select data and bind it to gridview control.

protected void btnsave_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(“Data Source=’.\\SQLEXPRESS’;Integrated Security=’true’;Initial Catalog=’Example'”);
SqlDataAdapter Adp = new SqlDataAdapter(“INSERT INTO UserMst values (‘” + txtname.Text +”‘,'”+  txtsurname.Text +”‘,'”+  txtcity.Text +”‘)”, con);
DataTable DT = new DataTable();
Adp.Fill(DT);

SqlDataAdapter Adpselect = new SqlDataAdapter(“select * from UserMst”, con);
Adpselect.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
}

Add manually create columns in gridview control

Add manually create columns in gridview control

I hope this gridview control add columns example will help you….

Paging in GridView Control in ASP.Net

Here, we learn how to do paging in gridview control in ASP.Net with C#.

we will learn paging in gridview with an example.

Some most importance thing in paging are:

AllowPaging = True

PageSize = int values

PageIndexChanging Event

First Create New ASP.Net website in Visual Studio…

Take one GridView control on webform

<asp:GridView ID=”GridView1″ runat=”server”
BackColor=”White” BorderColor=”#CC9966″ BorderStyle=”None” BorderWidth=”1px”
CellPadding=”4″ onselectedindexchanged=”GridView1_SelectedIndexChanged1″
AllowPaging=”True” onpageindexchanging=”GridView1_PageIndexChanging”
onselectedindexchanging=”GridView1_SelectedIndexChanging” PageSize=”5″>
<RowStyle BackColor=”White” ForeColor=”#330099″ />
<FooterStyle BackColor=”#FFFFCC” ForeColor=”#330099″ />
<PagerStyle BackColor=”#FFFFCC” ForeColor=”#330099″ HorizontalAlign=”Center” />
<SelectedRowStyle BackColor=”#FFCC66″ Font-Bold=”True” ForeColor=”#663399″ />
<HeaderStyle BackColor=”#990000″ Font-Bold=”True” ForeColor=”#FFFFCC” />
</asp:GridView>

Now, Set the AllowPaging = True of GridView control and assign Integer value to PageSize Property of GridView control.

In below scree shows the set AllowPaging=True og GridView control.

How to do paging in ASP.Net with C#

How to do paging in ASP.Net with C#

Here we set the PageSize of GridView control.
PageSize define the no. of record display per page on gridview control.

How to do paging in ASP.Net with C#

How to do paging in ASP.Net with C#

After doing this now, write some code at Gridview_PageIndexChanging event.

 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}

How to do paging in ASP.Net with C#

How to do paging in ASP.Net with C#

write below code at page_load event of webform:

 protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
BindGrid();
}
}

public void BindGrid()
{
DataSet1.AddressBook_SELECTDataTable DT = new DataSet1.AddressBook_SELECTDataTable();
DataSet1TableAdapters.AddressBook_SELECTTableAdapter Adapter = new DataSet1TableAdapters.AddressBook_SELECTTableAdapter();

DT = Adapter.SelectData();
GridView1.DataSource = DT;
GridView1.DataBind();
}

 

The output of Gridview control paging in asp.net is :

How to do paging in ASP.Net with C#

How to do paging in ASP.Net with C#

How to Set Auto Format in GridView Control in ASP.Net

Gridview control in asp.net is most important control in asp.net..

Here we learn how to use auto format on gridview in asp.net. We can use gridview in asp.net for multiple purpose generally we use gridview for display recode on web forms in asp.net.

first bring gridview control on web forms like below..

gridview control in asp.net

gridview control in asp.net

Now, show as above gridview asp.net figure select Auto Format option from top right corner of Gridview control.

we have here many ready made format for gridview control..select one of them…and apply ok…..

gridview control in asp.net

c#.net gridview example

 

After selecting format we have gridview output like below figure….now this way we can make color full gridview in asp.net without programming.

c#.net gridview example

asp.net gridview control,

 

We can use Gridview control with jquery grid and also use ready made telerik grid control in asp.net,

I hope this c#.net gridview example will help you……

Insert, Edit, Update, Delete Gridview in ASP.Net

–  In this Post we will learn how to Display Data in Gridview and Edit,Update and Delete from Gridview in ASP.Net.

– First we have to make Connection between SQL-Server and Our .Net website.

STEP 1-

– Create Database  in SQL-Server  DatabaseName: – Gridview
– Create Table in Database TabelName :- User_Mst

Create Table SQL-Server

– Create Stored_Procedure for SELECT, INSERT, UPDATE and DELETE in SQL-Server

Create Stored Procedure for SELECT Data

CREATE PROCEDURE [dbo].[USER_MST_SELECT]
AS
BEGIN
SELECT * FROM USER_MST-
END

Create Stored Procedure for INSERT Data

CREATE PROCEDURE [dbo].[USER_MST_INSERT]
@NAME AS NVARCHAR(256),
@SURNAME AS NVARCHAR(256),
@ADD AS NVARCHAR(256),
@CITY AS NVARCHAR(256),
@PIN AS INT,
@UNAME AS NVARCHAR(256),
@UPASSWORD AS NVARCHAR(256)
AS
BEGIN
INSERT INTO  USER_MST VALUES (@NAME,@SURNAME,@ADD,@CITY,@PIN,@UNAME,@UPASSWORD)
END

Create Stored Procedure for UPDATE Data

CREATE PROCEDURE [dbo].[USER_MST_update]
@name as nvarchar(256),
@surname as nvarchar(256),
@city as nvarchar(256),
@id as int
AS
BEGIN
update USER_MST set NAME=@name,SURNAME=@surname,CITY=@city where ID=@id
END

Create Stored Procedure for DELETE Data

CREATE PROCEDURE [dbo].[USER_MST_Delete]
@ID as int
AS
BEGIN
delete from USER_MST  WHERE ID=@ID
END

 

STEP 2 –

Now, Bind all Stored Procedure with .net application as we learn ago post, connection between asp.net and SQL-server.

Then, write below code in BODY tag for Design Page:-

<body>
<form id=”form1″ runat=”server”>
<div>
<table id=”TABLE1″ language=”javascript” onclick=”return TABLE1_onclick()” style=”z-index: 100;
left: 360px; position: absolute; top: 24px”>
<tr>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
NAME:</td>
<td style=”width: 100px”>
<asp:TextBox ID=”txtname” runat=”server”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
SURNAME:</td>
<td style=”width: 100px”>
<asp:TextBox ID=”txtsurname” runat=”server” ></asp:TextBox></td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
ADDRESS:</td>
<td style=”width: 100px”>
<asp:TextBox ID=”txtaddress” runat=”server”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
CITY:</td>
<td style=”width: 100px”>
<asp:TextBox ID=”txtcity” runat=”server”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px; height: 26px”>
PIN:</td>
<td style=”width: 100px; height: 26px”>
<asp:TextBox ID=”txtpin” runat=”server” ></asp:TextBox></td>
<td style=”width: 100px; height: 26px”>
</td>
<td style=”width: 100px; height: 26px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
USERNAME:</td>
<td style=”width: 100px”>
<asp:TextBox ID=”txtusername” runat=”server” ></asp:TextBox></td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
PASSWORD:</td>
<td style=”width: 100px”>
<asp:TextBox ID=”txtpassword” runat=”server” TextMode=”Password” Width=”152px”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px; height: 34px”>
</td>
<td style=”width: 100px; height: 34px”>
<asp:Button ID=”BTNSUBMIT” runat=”server” Font-Bold=”True” Height=”40px” OnClick=”BTNSUBMIT_Click”
Text=”SUBMIT” Width=”96px” /></td>
<td style=”width: 100px; height: 34px”>
</td>
<td style=”width: 100px; height: 34px”>
</td>
</tr>
<tr>
<td style=”width: 100px; height: 26px”>
</td>
<td style=”width: 100px; height: 26px”>
&nbsp;<asp:Label ID=”lblmsg” runat=”server” Font-Bold=”True” ForeColor=”#C04000″></asp:Label>
<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” AutoGenerateDeleteButton=”True”
AutoGenerateEditButton=”True” AutoGenerateSelectButton=”True” CellPadding=”4″
DataKeyNames=”id” ForeColor=”#333333″ GridLines=”None” OnRowCancelingEdit=”GridView1_RowCancelingEdit”
OnRowDeleting=”GridView1_RowDeleting” OnRowEditing=”GridView1_RowEditing”
OnRowUpdating=”GridView1_RowUpdating” >
<Columns>
<asp:TemplateField HeaderText=”Name”>
<ItemTemplate>
<asp:Label ID=”lblname” runat=”server” Text='<%#Eval(“name”) %>’>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=”txtname” runat=”server” Text='<%#Eval(“name”) %>’>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText=”SurName”>
<ItemTemplate>
<asp:Label ID=”lblsurname” runat=”server” Text='<%#Eval(“surname”) %>’>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=”txtsurname” runat=”server” Text='<%#Eval(“surname”) %>’>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText=”City”>
<ItemTemplate>
<asp:Label ID=”txtcity” runat=”server” Text='<%#Eval(“City”) %>’>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=”txtcity” runat=”server” Text='<%#Eval(“City”) %>’>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

</Columns>
<RowStyle BackColor=”#F7F6F3″ ForeColor=”#333333″ />
<FooterStyle BackColor=”#5D7B9D” Font-Bold=”True” ForeColor=”White” />
<PagerStyle BackColor=”#284775″ ForeColor=”White” HorizontalAlign=”Center” />
<SelectedRowStyle BackColor=”#E2DED6″ Font-Bold=”True” ForeColor=”#333333″ />
<HeaderStyle BackColor=”#5D7B9D” Font-Bold=”True” ForeColor=”White” />
<EditRowStyle BackColor=”#999999″ />
<AlternatingRowStyle BackColor=”White” ForeColor=”#284775″ />
</asp:GridView>
</td>
<td style=”width: 100px; height: 26px”>
</td>
<td style=”width: 100px; height: 26px”>
</td>
</tr>
<tr>
<td colspan=”3″>
&nbsp;&nbsp;
</td>
<td colspan=”1″>
</td>
</tr>
</table>

</div>
</form>
</body>

Design Gridview in ASP.Net

Design Gridview in ASP.Net

– Now, write below code in Page_load

DS_Grid.USERMST_SELECTDataTable GridDt = new DS_Grid.USERMST_SELECTDataTable();
DS_GridTableAdapters.USERMST_SELECTTableAdapter GridAdapter = new DS_GridTableAdapters.USERMST_SELECTTableAdapter();

protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
GridDt = GridAdapter.SelectUser();
GridView1.DataSource = GridDt;
GridView1.DataBind();
}
lblmsg.Text = “”;
}

– Insert Data into Gridview in ASP.Net

–   write below code in SUBMIT button Click_Event

protected void BTNSUBMIT_Click(object sender, EventArgs e)
{
int insetdata = GridAdapter.Insert(txtname.Text, txtsurname.Text, txtaddress.Text, txtcity.Text, Convert.ToInt32(txtpin.Text), txtusername.Text, txtpassword.Text);

GridDt = GridAdapter.SelectUser();
GridView1.DataSource = GridDt;
GridView1.DataBind();
lblmsg.Text = “Record Inserted Successfully !”;
}

– Now, Edit Data Gridview in ASP.Net

–  protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridDt = GridAdapter.SelectUser();
GridView1.DataSource = GridDt;
GridView1.DataBind();

}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridDt = GridAdapter.SelectUser();
GridView1.DataSource = GridDt;
GridView1.DataBind();
}

Edit Data in Gridview

Edit Data in Gridview

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView1.EditIndex = -1;
TextBox txtnewname = (TextBox)GridView1.Rows[e.RowIndex].FindControl(“txtname”);
TextBox txtnewsurname = (TextBox)GridView1.Rows[e.RowIndex].FindControl(“txtsurname”);
TextBox txtnewcity = (TextBox)GridView1.Rows[e.RowIndex].FindControl(“txtcity”);
GridAdapter.Update(txtnewname.Text, txtnewsurname.Text,txtnewcity.Text,Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value));

GridDt = GridAdapter.SelectUser();
GridView1.DataSource = GridDt;
GridView1.DataBind();
lblmsg.Text = “Record Updated Successfully !”;
}

Edit Data in Gridview

Edit Data in Gridview

– Now Delete Data from Gridview in ASP.Net

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
GridAdapter.Delete(Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value));
GridDt = GridAdapter.SelectUser();
GridView1.DataSource = GridDt;
GridView1.DataBind();
lblmsg.Text = “Record Deleted Successfully !”;

}

Detete Data from Gridview in ASP.Net

Detete Data from Gridview in ASP.Net

We hope this will helpful to all………………..

GridView in ASP.Net

– Here we are going to learn how to bind Data in Gridview control from SQL-Server Databse in ASP.Net with C#.

– GridView control is most important control in ASP.Net.

– GridView control used to Bind Data from Database and Display Data on webform.

STEP 1 – get the Gridview control on webform

 

Gridview in asp.net with c#

Gridview in asp.net with c#

<body>
<form id=”form1″ runat=”server”>
<div>
<table id=”TABLE1″ language=”javascript” onclick=”return TABLE1_onclick()” style=”z-index: 100;
left: 320px; position: absolute; top: 16px”>
<tr>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
NAME:</td>
<td style=”width: 100px”>
<asp:TextBox ID=”txtname” runat=”server”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
SURNAME:</td>
<td style=”width: 100px”>
<asp:TextBox ID=”txtsurname” runat=”server”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
ADDRESS:</td>
<td style=”width: 100px”>
<asp:TextBox ID=”txtaddress” runat=”server”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
CITY:</td>
<td style=”width: 100px”>
<asp:TextBox ID=”txtcity” runat=”server”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px; height: 26px”>
PIN:</td>
<td style=”width: 100px; height: 26px”>
<asp:TextBox ID=”txtpin” runat=”server”></asp:TextBox></td>
<td style=”width: 100px; height: 26px”>
</td>
<td style=”width: 100px; height: 26px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
USERNAME:</td>
<td style=”width: 100px”>
<asp:TextBox ID=”txtusername” runat=”server”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px”>
PASSWORD:</td>
<td style=”width: 100px”>
<asp:TextBox ID=”txtpassword” runat=”server” TextMode=”Password” Width=”152px”></asp:TextBox></td>
<td style=”width: 100px”>
</td>
<td style=”width: 100px”>
</td>
</tr>
<tr>
<td style=”width: 100px; height: 34px”>
</td>
<td style=”width: 100px; height: 34px”>
<asp:Button ID=”BTNSUBMIT” runat=”server” Font-Bold=”True” Height=”40px” OnClick=”BTNSUBMIT_Click”
Text=”SUBMIT” Width=”96px” /></td>
<td style=”width: 100px; height: 34px”>
</td>
<td style=”width: 100px; height: 34px”>
</td>
</tr>
<tr>
<td colspan=”4″ style=”height: 26px”>
&nbsp;<asp:GridView ID=”GridView1″ runat=”server” CellPadding=”4″ DataKeyNames=”id”
ForeColor=”#333333″ GridLines=”None”>
<RowStyle BackColor=”#F7F6F3″ ForeColor=”#333333″ />
<FooterStyle BackColor=”#5D7B9D” Font-Bold=”True” ForeColor=”White” />
<PagerStyle BackColor=”#284775″ ForeColor=”White” HorizontalAlign=”Center” />
<SelectedRowStyle BackColor=”#E2DED6″ Font-Bold=”True” ForeColor=”#333333″ />
<HeaderStyle BackColor=”#5D7B9D” Font-Bold=”True” ForeColor=”White” />
<EditRowStyle BackColor=”#999999″ />
<AlternatingRowStyle BackColor=”White” ForeColor=”#284775″ />
</asp:GridView>
</td>
</tr>
<tr>
<td colspan=”3″>
&nbsp;&nbsp;
</td>
<td colspan=”1″>
</td>
</tr>
</table>

</div>
</form>
</body>

STEP 2 – make connection with SQL Server Database and Design Page as above.

– Write below code in Submit Button control Click_Event

protected void BTNSUBMIT_Click(object sender, EventArgs e)
{
DS_Grid.USERMST_SELECTDataTable GridDt = new DS_Grid.USERMST_SELECTDataTable();
DS_GridTableAdapters.USERMST_SELECTTableAdapter GridAdapter = new DS_GridTableAdapters.USERMST_SELECTTableAdapter();

int insetdata = GridAdapter.Insert(txtname.Text, txtsurname.Text, txtaddress.Text, txtcity.Text, Convert.ToInt32(txtpin.Text), txtusername.Text, txtpassword.Text);

GridDt = GridAdapter.SelectUser();
GridView1.DataSource = GridDt;
GridView1.DataBind();
}

OutPut :-

Bind Gridview control in ASP.Net

ADD Select button in Gridview control in ASP.Net

ADD Select button in Gridview control in ASP.Net

STEP – 1 Gridview control provide system select button, just go to the Gridview property and set AutogenerateSelectButton property True.

ADD Select button in Gridview control in ASP.Net

ADD Select button in Gridview control in ASP.Net

STEP – 2  Now fire the SelectedIndexChanged Event for Select button on Gridview control.

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}

ADD Select button in Gridview control in ASP.Net

ADD Select button in Gridview control in ASP.Net

STEP –  3 Now go to the SelectedIndexchanged Event for handling coding for Select Button.

If we want to get the Value of selected row when push the select button.
we have to write our coding in selectedindexchanged event.

Suppose, we want to get the name of the selected row.
We first assign name column to Gridview control Datakeyname property.

<asp:GridView ID=”GridView1″ DataKeyNames=”name” runat=”server” Height=”216px” Width=”280px” AutoGenerateColumns=”False” AutoGenerateSelectButton=”True” OnSelectedIndexChanged=”GridView1_SelectedIndexChanged”>

<Columns>
<asp:BoundField HeaderText=”Name” DataField=”name” />
<asp:BoundField HeaderText=”Surname” DataField=”surname” />
<asp:BoundField HeaderText=”Address” DataField=”Address” />
</Columns>

</asp:GridView>

Now go to the selectedindexchanged event of Gridview control and write code for getting name of selected row.

ADD Select button in Gridview control in ASP.Net

ADD Select button in Gridview control in ASP.Net

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
  Label1.Text = GridView1.SelectedDataKey.Value.ToString();
}

How to add manually columns to Gridview in ASP.Net

How to add manually columns to Gridview in ASP.Net

If you want to become a good programmer in ASP.Net, so you must have to get knowledge about Gridview control in ASP.Net.

If bind Gridview control with Database, the Gridview control shows/Display the all columns of Database,
But some time we not need to show some column to user so we have to bind all column manually.

STEP – 1 For add manually column to Gridview first set the autogeneratecolumn property to false.

AutoGenerateColumns=”False”

Add manually columns to Gridview in ASP.Net

Add manually columns to Gridview in ASP.Net

STEP – 2 After set  AutoGenerateColumns=”False” we have to write below code in source of Gridview control for add manually column.

<asp:GridView ID=”GridView1″ runat=”server” Height=”216px” Width=”280px” AutoGenerateColumns=”False”>

<Columns>
<asp:BoundField HeaderText=”Name” DataField=”name” />
<asp:BoundField HeaderText=”Surname” DataField=”surname” />
<asp:BoundField HeaderText=”Address” DataField=”Addres” />
</Columns>

</asp:GridView>

 

Now run your website and get output with gridview manually column.

Add manually columns to Gridview in ASP.Net

Add manually columns to Gridview in ASP.Net

Bind Gridview in ASP.Net C#

Bind Gridview fron Database in ASP.Net C#

 

Gridview control is the one of the main control of ASP.Net.

Gridview control is used to display data from Database to user webform in specific format.

STEP – 1 First Bind data to gridview we have to make connection between Database and our ASP.Net website.

learn for connectivity visit – connectivity with Database with ASP.Net

 

STEP – 2 After making Connection take Gridview control on webform.

</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table style=”z-index: 100; left: 312px; position: absolute; top: 120px”>
<tr>
<td style=”width: 100px”>
<asp:GridView ID=”GridView1″ runat=”server” BackColor=”White” BorderColor=”#CC9966″ BorderStyle=”None” BorderWidth=”1px” CellPadding=”4″>
<RowStyle BackColor=”White” ForeColor=”#330099″ />
<FooterStyle BackColor=”#FFFFCC” ForeColor=”#330099″ />
<PagerStyle BackColor=”#FFFFCC” ForeColor=”#330099″ HorizontalAlign=”Center” />
<SelectedRowStyle BackColor=”#FFCC66″ Font-Bold=”True” ForeColor=”#663399″ />
<HeaderStyle BackColor=”#990000″ Font-Bold=”True” ForeColor=”#FFFFCC” />
</asp:GridView>
</td>
</tr>
</table>

</div>
</form>
</body>
</html>

 

Gridview in asp.net

Gridview in asp.net

 

STEP – 3 Now write the below code on page load

public partial class _Default : System.Web.UI.Page
{
// Declare DataTable and TableAdapter Object here..
User_DS.USERMST_SELECTDataTable USerDT = new User_DS.USERMST_SELECTDataTable();
User_DSTableAdapters.USERMST_SELECTTableAdapter UserAdapter = new User_DSTableAdapters.USERMST_SELECTTableAdapter();

protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
USerDT = UserAdapter.Select();

GridView1.DataSource = USerDT;
GridView1.DataBind();

}

}

}