AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 dropdownlist in gridview
Posted by merinkmathew on  2/15/2009 5:13:29 AM Category: Asp.Net 2.0
Add to my favorites
Email to friend
Hello! I am a newbie and need some help. I am working in Asp.Net 2.0 and using c#

i an using gridview with dropdownlist , its insert also working , my problem is while clicking edit , dropdown list not show current data, (eg ;; data is nnn they show aaaa, only edit show every time first value only), in display time they show id


pls go throuh my eg

<asp:GridView ID="GridView1" runat="server" CellPadding="4" DataKeyNames="Brandid"
ShowFooter="True" AutoGenerateColumns="False" OnRowUpdated="GridView1_RowUpdated"
OnRowUpdating="GridView1_RowUpdating" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit"
BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<RowStyle BackColor="White" ForeColor="#330099" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<Columns>
<asp:TemplateField HeaderText="Brandid" InsertVisible="False" SortExpression="Brandid">
<ItemTemplate>
<asp:Label ID="lblBrandid" runat="server" Text='<%# Eval("Brandid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Brand code" SortExpression="Brandcode" InsertVisible="False">
<EditItemTemplate>
<asp:TextBox ID="txtBrandcode" runat="server" Text='<%#Bind("Brandcode") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblBrandcode" runat="server" Text='<%#Eval("Brandcode") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtfooterBrandcode" runat="server"> </asp:TextBox>
<asp:RequiredFieldValidator ID="rfvProductCode" runat="server" ControlToValidate="txtfooterBrandcode"
ErrorMessage="*"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Brand name" InsertVisible="False" SortExpression="Brandname">
<EditItemTemplate>
<asp:TextBox ID="txtBrandname" runat="server" Text='<%#Bind("Brandname") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblBrandname" runat="server" Text='<%#Eval("Brandname") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtfooterBrandname" runat="server"> </asp:TextBox>
<asp:RequiredFieldValidator ID="rfvBrandname" runat="server" ControlToValidate="txtfooterBrandname"
ErrorMessage="*"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Brand description" InsertVisible="False" SortExpression="Branddescription">
<EditItemTemplate>
<asp:TextBox ID="txtBranddescription" runat="server" Text='<%#Bind("Branddescription") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblBranddescription" runat="server" Text='<%# Eval("Branddescription") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtfooterBranddescription" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvBranddescription" runat="server" ControlToValidate="txtfooterBranddescription"
ErrorMessage="*"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>


<asp:TemplateField HeaderText="CategoryName">
<EditItemTemplate>
<asp:DropDownList ID="dllBrandCategoryid" DataSource='<%# getCategoryName() %>' runat="server" DataTextField="CategoryName" DataValueField="CategoryName">
</asp:DropDownList>
&nbsp;<br />
&nbsp;<br />
</EditItemTemplate>

<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>

<%-- <ItemTemplate>
<asp:Label ID="lbCategoryName" runat="server" Text='<%# Eval("BrandCategoryid") %>'></asp:Label>
</ItemTemplate>--%>
<FooterTemplate>


<asp:DropDownList ID="dllfooterBrandCategoryid" DataSource='<%# getCategoryName() %>' runat="server" DataTextField="CategoryName" DataValueField="CategoryName">
</asp:DropDownList>
&nbsp; &nbsp;<br />
&nbsp;<br />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<EditItemTemplate>
<asp:LinkButton ID="lbkUpdate" runat="server" CausesValidation="False" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" Text="Insert" OnClick="lnkInsert_Click"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" HeaderText="Delete" />
</Columns>
</asp:GridView>
<asp:Label ID="lblMessage" runat="server" Text="Insert Record Successfully" Visible="false"> </asp:Label>
</div>
</form>
</body>
</html>



public partial class ADMIN_CUSTOMER_SUPPORT_SITE_Admin_AddBrand : System.Web.UI.Page
{

SqlCommand cmd = new SqlCommand();

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
Binddata();
bindGridView();
}

}



private void Binddata()
{

SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
SqlDataAdapter ad = new SqlDataAdapter("Select * from Brand", conn);

DataSet ds = new DataSet();

ad.Fill(ds, "Brand");

GridView1.DataSource = ds;

GridView1.DataBind();

}

public void bindGridView()
{

SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);


if (conn.State == ConnectionState.Closed)
{
conn.Open();
}


SqlCommand mySqlCommand = new SqlCommand("select * from Category", conn);
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);


GridView1.DataSource = myDataSet;
GridView1.DataBind();


mySqlCommand = new SqlCommand("select * from Brand", conn);
mySqlAdapter = new SqlDataAdapter(mySqlCommand);
myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);


DropDownList drdList;


foreach (GridViewRow grdRow in GridView1.Rows)
{
drdList = (DropDownList)(GridView1.Rows[grdRow.RowIndex].Cells[1].FindControl("DropDownList1"));

drdList.DataSource = myDataSet;
drdList.DataValueField = "Brandid";
drdList.DataTextField = "BrandCategoryid";
drdList.DataBind();

drdList.SelectedValue = GridView1.DataKeys[grdRow.RowIndex]["Brandid"].ToString();
}


if (conn.State == ConnectionState.Open)
{
conn.Close();
}

}



public DataSet getCategoryName()
{

SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);

SqlDataAdapter ad = new SqlDataAdapter("SELECT CategoryName FROM Category", conn);

DataSet ds = new DataSet();

ad.Fill(ds, "CategoryName");

return ds;

}


protected void lnkInsert_Click(object sender, EventArgs e)
{

TextBox txtfooterBrandcode = GridView1.FindControl("txtfooterBrandcode") as TextBox;
TextBox txtfooterBrandname = GridView1.FindControl("txtfooterBrandname") as TextBox;
DropDownList dllfooterBrandCategoryid = GridView1.FindControl("dllfooterBrandCategoryid") as DropDownList;
TextBox txtfooterBranddescription = GridView1.FindControl("txtfooterBranddescription") as TextBox;



SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
conn.Open();
cmd = new SqlCommand("insertBrand", conn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@Brandcode", SqlDbType.VarChar).Value = ((TextBox)GridView1.FooterRow.FindControl("txtfooterBrandcode")).Text;
cmd.Parameters.Add("@Brandname", SqlDbType.VarChar).Value = ((TextBox)GridView1.FooterRow.FindControl("txtfooterBrandname")).Text;
cmd.Parameters.Add("@Categoryname", SqlDbType.VarChar).Value = ((DropDownList)GridView1.FooterRow.FindControl("dllfooterBrandCategoryid")).SelectedValue;
cmd.Parameters.Add("@Branddescription", SqlDbType.VarChar).Value = ((TextBox)GridView1.FooterRow.FindControl("txtfooterBranddescription")).Text;

cmd.ExecuteNonQuery();
conn.Close();
Binddata();

}

protected void lbkUpdate_Click(object sender, EventArgs e)
{


}



protected void lnkCancel_Click(object sender, EventArgs e)
{


}



protected void lnkEdit_Click(object sender, EventArgs e)
{



}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{



}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

Label Brandid = (Label)GridView1.Rows[e.RowIndex].FindControl("lblBrandid");
TextBox Brandcode = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBrandcode");
TextBox Brandname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBrandname");

DropDownList BrandCategoryid = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("dllBrandCategoryid");
TextBox Branddescription = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBranddescription");

SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
conn.Open();
cmd = new SqlCommand("UpdateBrand", conn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@Brandid", SqlDbType.Int).Value = Brandid.Text;
cmd.Parameters.Add("@Brandcode", SqlDbType.VarChar).Value = Brandcode.Text;
cmd.Parameters.Add("@Brandname", SqlDbType.VarChar).Value = Brandname.Text;
cmd.Parameters.Add("@BrandCategoryid", SqlDbType.VarChar).Value = BrandCategoryid.SelectedValue;
cmd.Parameters.Add("@Branddescription", SqlDbType.VarChar).Value = Branddescription.Text;

cmd.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1;
Binddata();
}





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



protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Binddata();
}


protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{




}


}



here table brand have Brandid,Brandcode,Brandname,BrandCategoryid,Branddescription


here BrandCategoryid comes from Category table ..


pls sent code

Post Your Reply Post New Question

 Viewer's Reply
dropdownlist in gridview
Answered By Kunwar on 3/4/2009 4:13:00 AM
This code piece in BindData is where you are binding the dropdownlist with dataset
mySqlCommand = new SqlCommand("select * from Brand order by BrandId desc", conn);
//This way it will always populate the newly added Item as selected--
foreach (GridViewRow grdRow in GridView1.Rows)
{
drdList = (DropDownList)(GridView1.Rows[grdRow.RowIndex].Cells[1].FindControl("DropDownList1"));

drdList.DataSource = myDataSet;
drdList.DataValueField = "Brandid";
drdList.DataTextField = "BrandCategoryid";
drdList.DataBind();

drdList.SelectedValue = GridView1.DataKeys[grdRow.RowIndex]["Brandid"].ToString();
}

Kunwar
http://KpratapS.blogspot.com
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Web Application Security — How to Minimize Prevalent Risk of Attacks
Building a Web Application Security Program
Introduction to Software Development