how to add dropdown in gridview using editmode

Posted by Klbaiju under ASP.NET on 4/4/2012 | Points: 10 | Views : 5535 | Status : [Member] | Replies : 1
Hi,
in my application there is a gridview,
and fields
id,uname,password,utype.

edit mode is working.
i need in edit mode utype should become a dropdownlist and that contains admin ,manager,ceo.
i need dropdown only in edit mode.don't show that gridview display.

how it is possible.


Regards

Baiju




Responses

Posted by: Self-Innovator on: 4/4/2012 [Member] Bronze | Points: 25

Up
0
Down
hi try like this...
In design .aspx
<asp:GridView ID="gvPerson" ShowFooter="true" RowStyle-VerticalAlign="top" DataKeyNames="id" runat="server" AutoGenerateColumns="False" OnRowEditing="gvPerson_RowEditing" OnRowDataBound="gvPerson_RowDataBound" OnRowUpdating="gvPerson_RowUpdating">

<Columns>
<asp:TemplateField HeaderText="Country">
<ItemTemplate><%#Eval("PersonCountry") %></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="NewddlCountry" AutoPostBack="true" OnSelectedIndexChanged="NewddlCountry_SelectedIndexChanged" runat="server"></asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" ValidationGroup="v2" runat="server" ControlToValidate="NewddlCountry" ErrorMessage="Select Country"></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddNew" OnClick="btnAddNew_Click" runat="server" Text="AddNew" />
</FooterTemplate>
</asp:TemplateField>
</asp:GridView>


In page Load Bind Grid

protected void Page_Load(object sender, EventArgs e)

{
if (!Page.IsPostBack)
{
FillCountries();
}
}
protected void gvPerson_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 NewddlCountry = (DropDownList)e.Row.FindControl("NewddlCountry");
DataSet tempCountries = BAL.GetCountries();
NewddlCountry.DataSource = tempCountries;
NewddlCountry.DataTextField = "CountryName";
NewddlCountry.DataValueField = "CountryCode";
NewddlCountry.DataBind();
for (int i = 0; i < NewddlCountry.Items.Count; i++)
{
if (NewddlCountry.Items[i].Text.Trim().CompareTo(SelectedCounty.Trim()) == 0)
{
NewddlCountry.SelectedIndex = i;
break;
}
}
}
DATALAYER
public DataSet GetCountries()
{
cnn = new SqlConnection(Conn);
cmd = new SqlCommand("sp_GetCountryList", cnn);
cmd.CommandType = CommandType.StoredProcedure;
ada = new SqlDataAdapter(cmd);
ds = new DataSet();
ada.Fill(ds);
return ds;
}




Join Hands Change lives
Thanks & Regards
Straight Edge Society

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response