Introduction: In this post I will explain how we can edit
and update the Dropdownlist control in Gridview Data control.
Description:
I have created two table names QUALIFICATION and CANDIDATE_DETAIL.
ID
|
int
|
CANDIDATE_QUALIFICATION
|
varchar(50)
|
ID is primary key.
CANDIDATE_ID
|
int
|
CANDIDATE_NAME
|
varchar(50)
|
CANDIDTAE_QUALIFICATION
|
varchar(50)
|
PREFER_CITY
|
varchar(50)
|
JOB_PROFILE
|
varchar(50)
|
CANDIDATE_ID is primary key.
Now open the Visual Studio>Go to File>New>Website.
Add the Connectionstring in web.config file of website.
<configuration>
<connectionStrings>
<add name="connection" connectionString="Data Source=SYS-1F78031ED0A;Initial Catalog=TestBlog;Integrated
Security=True"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
After that add new web form to website, drag and drop the
Gridview data control from Toolbox. After that add the Template field and ItemTemplate
in Gridview as structure mention below:
<asp:GridView ID="grdcandidate"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="CANDIDATE_ID"
onrowdatabound="grdcandidate_RowDataBound"
onrowdeleting="grdcandidate_RowDeleting"
onrowediting="grdcandidate_RowEditing" onrowupdating="grdcandidate_RowUpdating"
AllowPaging="True" onpageindexchanging="grdcandidate_PageIndexChanging" onrowcancelingedit="grdcandidate_RowCancelingEdit"
>
<Columns>
<asp:TemplateField HeaderText="Candidate Name">
<EditItemTemplate>
<asp:TextBox ID="txtcandidtaename" runat="server" Text='<%# Eval("CANDIDATE_NAME") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcandidtae" runat="server" Text='<%# Eval("CANDIDATE_NAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Candidate Qualification">
<EditItemTemplate>
<asp:DropDownList ID="ddlqualification" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblqualification" runat="server"
Text='<%# Eval("CANDIDTAE_QUALIFICATION") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<EditItemTemplate>
<asp:TextBox ID="txtcity" runat="server" Text='<%# Eval("PREFER_CITY") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcity" runat="server" Text='<%# Eval("PREFER_CITY") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Job Profile">
<EditItemTemplate>
<asp:TextBox ID="txtjob" runat="server" Text='<%# Eval("JOB_PROFILE") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbljob" runat="server" Text='<%# Eval("JOB_PROFILE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
Now go to .aspx.cs page.
using
System.Data;
using
System.Data.SqlClient;
using
System.Configuration;
SqlConnection con
= new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
private void
Bindgridview()
{
DataTable
dt = new DataTable();
try
{
SqlDataAdapter
adp = new SqlDataAdapter("Select * from CANDIDATE_DETAIL", con);
adp.Fill(dt);
if
(dt.Rows.Count > 0)
{
grdcandidate.DataSource = dt;
grdcandidate.DataBind();
}
else
{
dt.Rows.Add(dt.NewRow());
grdcandidate.DataSource = dt;
grdcandidate.DataBind();
int
columncount = grdcandidate.Rows[0].Cells.Count;
grdcandidate.Rows[0].Cells.Clear();
grdcandidate.Rows[0].Cells.Add(new TableCell());
grdcandidate.Rows[0].Cells[0].ColumnSpan = columncount;
grdcandidate.Rows[0].Cells[0].Text = "No
Records Available";
}
}
catch (Exception ex)
{
}
finally
{
dt.Clear();
dt.Dispose();
dt = new
DataTable();
}
}
After that go to
Gridview Properties>Events and double click one by one on RowEditing,
RowCancelingEdit, RowUpdating ,RowDeleting and RowDatabound that will redirect
you to .aspx.cs page. Write the code for each event.
protected void grdcandidate_RowDataBound(object sender, GridViewRowEventArgs
e)
{
DataTable
dt = new DataTable();
try
{
if
(e.Row.RowType == DataControlRowType.DataRow
&& grdcandidate.EditIndex == e.Row.RowIndex)
{
SqlDataAdapter
filldrop = new SqlDataAdapter("Select * from QUALIFICATION", con);
DropDownList
ddl = (DropDownList)e.Row.FindControl("ddlqualification");
filldrop.Fill(dt);
ddl.DataSource = dt;
ddl.DataTextField = "CANDIDATE_QUALIFICATION";
ddl.DataValueField = "ID";
ddl.Items.Insert(0, "--Select--");
ddl.DataBind();
}
}
catch (Exception
ex)
{
}
finally
{
dt.Clear();
dt.Dispose();
dt = new
DataTable();
}
}
protected void grdcandidate_RowEditing(object
sender, GridViewEditEventArgs e)
{
grdcandidate.EditIndex =
e.NewEditIndex;
Bindgridview();
}
protected void grdcandidate_RowUpdating(object sender, GridViewUpdateEventArgs
e)
{
try
{
int
CANDIDATE_ID = Convert.ToInt32(grdcandidate.DataKeys[e.RowIndex].Value.ToString());
TextBox
txtname = (TextBox)grdcandidate.Rows[e.RowIndex].FindControl("txtcandidtaename");
TextBox
txtcity = (TextBox)grdcandidate.Rows[e.RowIndex].FindControl("txtcity");
TextBox
txtjob = (TextBox)grdcandidate.Rows[e.RowIndex].FindControl("txtjob");
DropDownList
ddlq = (DropDownList)grdcandidate.Rows[e.RowIndex].FindControl("ddlqualification");
string
Update = "Update CANDIDATE_DETAIL set
CANDIDATE_NAME='" + txtname.Text + "',
PREFER_CITY='" + txtcity.Text + "',JOB_PROFILE='"
+ txtjob.Text + "',CANDIDTAE_QUALIFICATION='"
+ ddlq.SelectedItem.ToString() + "' where
CANDIDATE_ID=" + CANDIDATE_ID;
SqlCommand
cmd = new SqlCommand(Update,
con);
cmd.ExecuteNonQuery();
grdcandidate.EditIndex = -1;
Bindgridview();
}
catch (Exception ex)
{
}
}
protected void grdcandidate_RowDeleting(object sender, GridViewDeleteEventArgs
e)
{
try
{
int
CANDIDATE_ID = Convert.ToInt32(grdcandidate.DataKeys[e.RowIndex].Value.ToString());
string
Delete = "Delete from CANDIDATE_DETAIL where
CANDIDATE_ID=" + CANDIDATE_ID;
SqlCommand
cmd = new SqlCommand(Delete,
con);
cmd.ExecuteNonQuery();
Bindgridview();
Messagebox("Record
Delete Successfully");
}
catch (Exception ex) { }
}
//To message
private void Messagebox(string
Message)
{
Label
lblMessageBox = new Label();
lblMessageBox.Text =
"<script
language='javascript'>" + Environment.NewLine
+
"window.alert('"
+ Message + "')</script>";
Page.Controls.Add(lblMessageBox);
}
protected void grdcandidate_RowCancelingEdit(object sender, GridViewCancelEditEventArgs
e)
{
grdcandidate.EditIndex = -1;
Bindgridview();
}
protected void grdcandidate_PageIndexChanging(object sender, GridViewPageEventArgs
e)
{
grdcandidate.PageIndex =
e.NewPageIndex;
Bindgridview();
}
In VB
Imports
System.Data
Imports
System.Data.SqlClient
Imports
System.Configuration
Dim con
As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())
Protected Sub Page_Load(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles
Me.Load
If
con.State = ConnectionState.Closed Then
con.Open()
End If
If Not IsPostBack Then
Bindgridview()
End If
End Sub
Private Sub
Bindgridview()
Dim dt As New DataTable()
Try
Dim
adp As New SqlDataAdapter("Select
* from CANDIDATE_DETAIL", con)
adp.Fill(dt)
If
dt.Rows.Count > 0 Then
grdcandidate.DataSource = dt
grdcandidate.DataBind()
Else
dt.Rows.Add(dt.NewRow())
grdcandidate.DataSource = dt
grdcandidate.DataBind()
Dim
columncount As Integer
= grdcandidate.Rows(0).Cells.Count
grdcandidate.Rows(0).Cells.Clear()
grdcandidate.Rows(0).Cells.Add(New TableCell())
grdcandidate.Rows(0).Cells(0).ColumnSpan = columncount
grdcandidate.Rows(0).Cells(0).Text = "No
Records Available"
End
If
Catch
ex As Exception
Finally
dt.Clear()
dt.Dispose()
dt = New
DataTable()
End Try
End Sub
Protected Sub grdcandidate_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim dt As New DataTable()
Try
If
e.Row.RowType = DataControlRowType.DataRow AndAlso grdcandidate.EditIndex = e.Row.RowIndex Then
Dim
filldrop As New
SqlDataAdapter("Select
* from QUALIFICATION", con)
Dim
ddl As DropDownList
= DirectCast(e.Row.FindControl("ddlqualification"), DropDownList)
filldrop.Fill(dt)
ddl.DataSource = dt
ddl.DataTextField = "CANDIDATE_QUALIFICATION"
ddl.DataValueField = "ID"
ddl.DataBind()
ddl.Items.Insert(0, "--Select--")
End
If
Catch
ex As Exception
Finally
dt.Clear()
dt.Dispose()
dt = New
DataTable()
End Try
End Sub
Protected Sub grdcandidate_RowEditing(ByVal
sender As Object,
ByVal e As GridViewEditEventArgs)
grdcandidate.EditIndex = e.NewEditIndex
Bindgridview()
End Sub
Protected Sub grdcandidate_RowUpdating(ByVal
sender As Object,
ByVal e As GridViewUpdateEventArgs)
Try
Dim
CANDIDATE_ID As Integer
= Convert.ToInt32(grdcandidate.DataKeys(e.RowIndex).Value.ToString())
Dim
txtname As TextBox
= DirectCast(grdcandidate.Rows(e.RowIndex).FindControl("txtcandidtaename"), TextBox)
Dim
txtcity As TextBox
= DirectCast(grdcandidate.Rows(e.RowIndex).FindControl("txtcity"), TextBox)
Dim
txtjob As TextBox
= DirectCast(grdcandidate.Rows(e.RowIndex).FindControl("txtjob"), TextBox)
Dim
ddlq As DropDownList
= DirectCast(grdcandidate.Rows(e.RowIndex).FindControl("ddlqualification"), DropDownList)
Dim
Update As String
= (("Update CANDIDATE_DETAIL set
CANDIDATE_NAME='" + txtname.Text & "',
PREFER_CITY='") + txtcity.Text & "',JOB_PROFILE='")
+ txtjob.Text & "',CANDIDTAE_QUALIFICATION='"
& ddlq.SelectedItem.ToString() & "'
where CANDIDATE_ID=" & CANDIDATE_ID
Dim
cmd As New SqlCommand(Update, con)
cmd.ExecuteNonQuery()
grdcandidate.EditIndex = -1
Bindgridview()
Catch
ex As Exception
End Try
End Sub
Protected Sub grdcandidate_RowDeleting(ByVal
sender As Object,
ByVal e As GridViewDeleteEventArgs)
Try
Dim
CANDIDATE_ID As Integer
= Convert.ToInt32(grdcandidate.DataKeys(e.RowIndex).Value.ToString())
Dim
Delete As String
= "Delete from CANDIDATE_DETAIL where
CANDIDATE_ID=" & CANDIDATE_ID
Dim
cmd As New SqlCommand(Delete, con)
cmd.ExecuteNonQuery()
Bindgridview()
Messagebox("Record
Delete Successfully")
Catch
ex As Exception
End Try
End Sub
'To message
Private Sub Messagebox(ByVal
Message As String)
Dim
lblMessageBox As New
Label()
lblMessageBox.Text = "<script language='javascript'>" +
Environment.NewLine & "window.alert('" & Message & "')</script>"
Page.Controls.Add(lblMessageBox)
End Sub
Protected Sub grdcandidate_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
grdcandidate.EditIndex = -1
Bindgridview()
End Sub
Protected Sub grdcandidate_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
grdcandidate.PageIndex = e.NewPageIndex
Bindgridview()
End Sub
Now debug the project
and check the result.
Ø How to bind Gridview using Sqldataadapter, Datatable andQuery in Asp.net
Ø How to highlight row on mouse hover in Gridview
Ø How to Search Records in Gridview in Asp.net
Ø How to Bind Gridview using Store Procedure, SqlDataAdapterand Datatable in Asp.net
Ø How to use RadioButtonList control inside the Gridview inAsp.net
Related Articles on Gridview:
No comments:
Post a Comment