Tuesday, July 7, 2009

Gridview With DropdownList Update



<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound"
onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Edit"
ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="btnedit"
runat="server"
CommandName="Edit" Text="Edit" ></asp:
LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnupdate" runat="server"
CommandName="Update" Text="Update"
></asp:LinkButton>
<asp:LinkButton ID="btncancel" runat="server"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="id">
<ItemTemplate>
<asp:Label ID="id" runat="server"
Text='<%#Eval("id") %
>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>
<asp:TemplateField HeaderText="city">
<ItemTemplate>
<asp:Label ID="city" runat="server"
Text='<%#Eval("city")
%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="dropcity" runat="server"></
asp:DropDownList>
<asp:RequiredFieldValidator ID="vali" runat="server"
ControlToValidate="dropcity"
ErrorMessage="*"><
/asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>

</Columns>

</asp:GridView>



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace dropdownwithgrid
{
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection("Data Source=.;
Initial Catalog=abhishek;Persist Security Info=True;User ID=sa;
Password=sa");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddata();

}
}

public void binddata()
{
DataTable dt = new DataTable();

sqlcon.Open();
SqlDataAdapter sda = new SqlDataAdapter();
string strQuery = "select id,city from id ";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlcon;
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
sqlcon.Close();
}

protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
Label l1 = (Label)GridView1.Rows[e.RowIndex].FindControl("id");
DropDownList d1 = (DropDownList)GridView1.Rows[e.RowIndex].
FindControl("dropcity");
sqlcon.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlcon;
cmd.CommandText = "update id set city='" +
d1.SelectedValue.ToString() + "' where id='" + l1.Text + "'";
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
binddata();
}

protected void GridView1_RowEditing(object sender,
GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
binddata();

}
public DataTable load_City()
{
// sqlcon.Open();
DataTable dt = new DataTable();


string sql = "select city from id";
SqlCommand cmd = new SqlCommand(sql);
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlcon;
SqlDataAdapter sd = new SqlDataAdapter(cmd);
sd.Fill(dt);

return dt;


}
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
DataRowView drv = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList dp = (DropDownList)e.Row.FindControl("dropcity");
DataTable dt = load_City();
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem lt = new ListItem();
lt.Text = dt.Rows[i][0].ToString();
dp.Items.Add(lt);
}
dp.SelectedValue = drv[1].ToString();
}
}

}
}
}

0 comments:

Post a Comment

plzz give the comment