Sunday, June 19, 2011

Pre Selecting Items In Dropdownlist In Gridview Edit Mode


<asp:TemplateField HeaderText="State">
<ItemTemplate>
<asp:Label ID="lblState" runat="server" Text='<%#Eval("TState") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlState" CssClass="textTableElement" AutoPostBack="true"

ToolTip='<%#Container.DataItemIndex%>' runat="server"

OnSelectedIndexChanged="ddlState_SelectedIndexChanged"> </asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
-----------------------------------------------------------------

protected void grdData_RowEditing(object sender, GridViewEditEventArgs e)
{
grdData.EditIndex = e.NewEditIndex;
BindGrid();

GridViewRow grow = (GridViewRow)grdData.Rows[e.NewEditIndex];

DropDownList ddl = new DropDownList();
ddl = (DropDownList)grow.FindControl("ddlCat");
ddl.DataSource = GetCategory();
ddl.DataTextField = "categoryname";
ddl.DataValueField = "categoryid";
ddl.DataBind();

string str = ViewState["cat"].ToString();

ddl.Items.FindByText(str).Selected = true;

}
--------------------------------------------

protected void grdData_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "edit")
{
GridViewRow grow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
Int32 userid = Convert.ToInt32(e.CommandArgument.ToString());
ViewState["userid"] = userid;

Label lblCat = new Label();
lblCat = (Label)grow.FindControl("lblcategory");

ViewState["cat"] = lblCat.Text.ToString(); // used in row_editing event to preselect the previous item

}

//******************
if (e.CommandName == "update")
{
GridViewRow grow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

DropDownList ddl = (DropDownList)grow.FindControl("ddlCat");

ddl.DataSource = GetCategory(); // function returns a datatable
ddl.DataTextField = "categoryname";
ddl.DataValueField = "categoryid";
ddl.DataBind();

Int32 userid = Convert.ToInt32(ViewState["userid"]);
Int32 catid = Convert.ToInt32(ViewState["categoryid"]);
Int32 intresult;

SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["conPractice"].ToString();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UpdateUser";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;

SqlParameter param = new SqlParameter();

cmd.Parameters.AddWithValue("@userid", userid);
cmd.Parameters.AddWithValue("@categoryid", catid);
param = cmd.Parameters.Add("ReturnValue", SqlDbType.Int);
param.Direction = ParameterDirection.ReturnValue;
con.Open();
cmd.ExecuteNonQuery();

intresult =(int) cmd.Parameters["ReturnValue"].Value;
con.Close();

if (intresult == 1)
{
lblmsg.Text="Updated Successfully!";

grdData.EditIndex = -1;
BindGrid();
}
if (intresult == -1)
{
lblmsg.Text = "Not Updated!!";
}
}

if (e.CommandName == "delete")
{

GridViewRow grow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
}
}

-------------------------------------------------------------------------------------

protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlState;
ddlState = (DropDownList)sender;
string state = ddlState.SelectedItem.Text;

ViewState["state"] = state;

DropDownList ddlCounty;
ddlCounty = (DropDownList)grdTerritory.Rows[Convert.ToInt32
(ddlState.ToolTip)].FindControl("ddlCounty");

clsBuilder.bind_County(ddlCounty, state);

}

-------------------------------------------------------------------------------


0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More