POC on Grid View with TextBox ,DropDown and Button
POC on Grid View with TextBox ,DropDown and Button
A proof of concept on populating a grid view with data from the database .There is a dropdown which takes the list from the database . The textbox present in the gridview is used to add countries in the list of country in the database .On click of the button event the "grdView_RowCommand" is called and respective operation is performed.
ASPX file
<%@ Page Title="Home Page" Language="C#" CodeBehind="Default.aspx.cs" Inherits="Indexers2._Default" %>
<form id="form1" runat="server">
<asp:GridView ID = "grdView"
runat = "server"
CellPadding = "5"
AutoGenerateColumns="false"
onrowcommand="grdView_RowCommand"
AutoGenerateSelectValue="true"
>
<Columns>
<asp:BoundField HeaderText="Name" DataField="CONTACTNAME" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:DropDownList ID = "drpdwnlst"
runat = "server"
>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:TextBox ID = "txtbx"
runat = "server"
>
</asp:TextBox>
</ItemTemplate>
</asp:Templatefield>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button
Id= "btn"
runat = "server"
Text = "Save"
CommandName = "SaveData"
CommandArgument ="<%#((GridViewRow) Container).RowIndex %>"
>
</asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
<form id="form1" runat="server">
<asp:GridView ID = "grdView"
runat = "server"
CellPadding = "5"
AutoGenerateColumns="false"
onrowcommand="grdView_RowCommand"
AutoGenerateSelectValue="true"
>
<Columns>
<asp:BoundField HeaderText="Name" DataField="CONTACTNAME" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:DropDownList ID = "drpdwnlst"
runat = "server"
>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:TextBox ID = "txtbx"
runat = "server"
>
</asp:TextBox>
</ItemTemplate>
</asp:Templatefield>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button
Id= "btn"
runat = "server"
Text = "Save"
CommandName = "SaveData"
CommandArgument ="<%#((GridViewRow) Container).RowIndex %>"
>
</asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
CS file
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Indexers2
{
public partial class _Default : Page
{
public string str;
public string cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//EVENT DELEGATE FOR THE GRID VIEW//
grdView.RowDataBound += grdView_RowDataBound;
grdView.DataSource = GetData("SELECT CONTACTNAME,COUNTRY FROM people");
grdView.DataBind();
}
}
//EVENT HANDLER FOR ROW DATA BOUND .WHEN ALL THE ROWS BIND TO THE GRID VIEW
void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
/* DROPDOWNLIST BEING POPULATED FROM THE DATABASE .EVEN IF THE DROP DOWN IS INSIDE A GRID VIEW*/
/* ON ROW DATA BOUND ONLY WE HAVE TO ACCESS THE VALUE USING GRIDVIEWROWEVENTARS OBJECT*/
DropDownList drpcountries = (e.Row.FindControl("drpdwnlst") as DropDownList);
drpcountries.DataSource = GetData("SELECT DISTINCT COUNTRY from people union SELECT DISTINCT CNTRY FROM COUNTRY");
drpcountries.DataTextField = "Country";
drpcountries.DataValueField = "Country";
drpcountries.DataBind();
drpcountries.Items.Insert(0, new ListItem("Please Select"));
//* CODE TO SHOW WHEN VALUE FROM DATABASE NEEDS TO BE SHOWED IN THE DROPDOWN LIST *//
// string country=(e.Row.FindControl("Country") as Label).Text;
// drpcountries.Items.FindByValue(country).Selected=true;
}
}
//EVENT HANDLER WHEN THE BUTTON/OR ANY CLICK EVENT HAPPENS INSIDE THE GRID
protected void grdView_RowCommand(object sender, GridViewCommandEventArgs e)
{
//CHECKING THE ELEMENT THAT CAUSE THE EVENT. HERE SaveData IS THE COMANDNAME MENTIONED IN THE UI
//BASED UPON WHICH THE SEARCH IS CARRIED OUT
if (e.CommandName == "SaveData")
{
//IDENTIFYING THE ROW IN THE GRID THAT CAUSED THE EVENT
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = grdView.Rows[index];
//GETTING THE TEXTBOX INPUT FROM THE GRID VIEW
string tbx_ = ((TextBox)grdView.Rows[index].FindControl("txtbx")).Text;
string cmd_ = tbx_;
SetData(cmd_);
}
}
//METHOD RESPONSIBLE FOR INSERTING THE DATA INTO THE DATA BASE TABLE
public void SetData(String sqlstmt)
{
string constring = ConfigurationManager.ConnectionStrings["ConnectionStringTest"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constring))
{
SqlCommand cmd = new SqlCommand("INSERT INTO COUNTRY VALUES ('" + sqlstmt + "')", conn);
cmd.CommandType = CommandType.Text;
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception e)
{
}
}
}
public DataSet GetData(String sqlstmt)
{
string constring = ConfigurationManager.ConnectionStrings["ConnectionStringTest"].ConnectionString;
SqlCommand cmd = new SqlCommand(sqlstmt);
using (SqlConnection conn = new SqlConnection(constring))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = conn;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
}
}
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Indexers2
{
public partial class _Default : Page
{
public string str;
public string cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//EVENT DELEGATE FOR THE GRID VIEW//
grdView.RowDataBound += grdView_RowDataBound;
grdView.DataSource = GetData("SELECT CONTACTNAME,COUNTRY FROM people");
grdView.DataBind();
}
}
//EVENT HANDLER FOR ROW DATA BOUND .WHEN ALL THE ROWS BIND TO THE GRID VIEW
void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
/* DROPDOWNLIST BEING POPULATED FROM THE DATABASE .EVEN IF THE DROP DOWN IS INSIDE A GRID VIEW*/
/* ON ROW DATA BOUND ONLY WE HAVE TO ACCESS THE VALUE USING GRIDVIEWROWEVENTARS OBJECT*/
DropDownList drpcountries = (e.Row.FindControl("drpdwnlst") as DropDownList);
drpcountries.DataSource = GetData("SELECT DISTINCT COUNTRY from people union SELECT DISTINCT CNTRY FROM COUNTRY");
drpcountries.DataTextField = "Country";
drpcountries.DataValueField = "Country";
drpcountries.DataBind();
drpcountries.Items.Insert(0, new ListItem("Please Select"));
//* CODE TO SHOW WHEN VALUE FROM DATABASE NEEDS TO BE SHOWED IN THE DROPDOWN LIST *//
// string country=(e.Row.FindControl("Country") as Label).Text;
// drpcountries.Items.FindByValue(country).Selected=true;
}
}
//EVENT HANDLER WHEN THE BUTTON/OR ANY CLICK EVENT HAPPENS INSIDE THE GRID
protected void grdView_RowCommand(object sender, GridViewCommandEventArgs e)
{
//CHECKING THE ELEMENT THAT CAUSE THE EVENT. HERE SaveData IS THE COMANDNAME MENTIONED IN THE UI
//BASED UPON WHICH THE SEARCH IS CARRIED OUT
if (e.CommandName == "SaveData")
{
//IDENTIFYING THE ROW IN THE GRID THAT CAUSED THE EVENT
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = grdView.Rows[index];
//GETTING THE TEXTBOX INPUT FROM THE GRID VIEW
string tbx_ = ((TextBox)grdView.Rows[index].FindControl("txtbx")).Text;
string cmd_ = tbx_;
SetData(cmd_);
}
}
//METHOD RESPONSIBLE FOR INSERTING THE DATA INTO THE DATA BASE TABLE
public void SetData(String sqlstmt)
{
string constring = ConfigurationManager.ConnectionStrings["ConnectionStringTest"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constring))
{
SqlCommand cmd = new SqlCommand("INSERT INTO COUNTRY VALUES ('" + sqlstmt + "')", conn);
cmd.CommandType = CommandType.Text;
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception e)
{
}
}
}
public DataSet GetData(String sqlstmt)
{
string constring = ConfigurationManager.ConnectionStrings["ConnectionStringTest"].ConnectionString;
SqlCommand cmd = new SqlCommand(sqlstmt);
using (SqlConnection conn = new SqlConnection(constring))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = conn;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
}
}
Database file
create table people
(
CONTACTNAME varchar(30),
COUNTRY varchar(30)
)
INSERT INTO people values('AMIT','INDIA');
INSERT INTO people values('AJAY','INDIA');
INSERT INTO people values('AJIT','INDIA');
INSERT INTO people values('AJIT_K','INDONESIA');
INSERT INTO people values('AJIT_M','BURMA');
(
CONTACTNAME varchar(30),
COUNTRY varchar(30)
)
INSERT INTO people values('AMIT','INDIA');
INSERT INTO people values('AJAY','INDIA');
INSERT INTO people values('AJIT','INDIA');
INSERT INTO people values('AJIT_K','INDONESIA');
INSERT INTO people values('AJIT_M','BURMA');
create table COUNTRY
(
CNTRY varchar(30),
)
(
CNTRY varchar(30),
)
INSERT INTO COUNTRY VALUES ('AMERICA');
INSERT INTO COUNTRY VALUES ('JAPAN');
INSERT INTO COUNTRY VALUES ('UNITED KINGDOM');
INSERT INTO COUNTRY VALUES ('JAPAN');
INSERT INTO COUNTRY VALUES ('UNITED KINGDOM');
WebConfig FIle
<connectionStrings>
<add name="ConnectionStringTest" providerName="Syste,.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=H:\MvcApplication1\MvcApplication1\MvcApplication1\App_Data\Models_-20150625114416.mdf;Integrated Security=True;Connect Timeout=30"/>
</connectionStrings>
<add name="ConnectionStringTest" providerName="Syste,.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=H:\MvcApplication1\MvcApplication1\MvcApplication1\App_Data\Models_-20150625114416.mdf;Integrated Security=True;Connect Timeout=30"/>
</connectionStrings>
Comments
Post a Comment