I am not able to get preselected text in dropdown in edit template. Please see my code:
<EditItemTemplate>
<asp:DropDownList ID="droplist" runat="server">
</asp:DropDownList>
</EditItemTemplate>
c# code
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList droplist = (DropDownList)e.Row.FindControl("droplist");
droplist.DataSource = EquipmentBLL.getunitdrop();
droplist.DataTextField = "UnitName";
droplist.DataValueField = "UnitID";
droplist.DataBind();
droplist.Items.Insert(0, new ListItem(" Select Unit ", "0"));
//droplist.Items.FindByText(unittypetext).Selected = true;
}
}
}
Can someone tell me what I should do to get preselected dropdown?
regards
Hussain
Right now, you're populating the DropDownList with options from a datasource. However, you're not binding it's selected value to anything.
Whatever you are doing to bind the other fields in your Gridview, do that for the DropDownList's SelectedValue as well.
Without seeing the rest of your GridView markup, I'm thinking something like this should work:
<EditItemTemplate>
<asp:DropDownList ID="droplist" runat="server"
SelectedValue='<%# Bind("UnitID") %>' >
</asp:DropDownList>
</EditItemTemplate>
Where "UnitID" above is the name of the field from your GridView's datasource that you want to bind to the SelectedValue of the DropDownList.
Related
I have a simple checkBox in Editable gridView :
<asp:TemplateField HeaderText="Editable">
<ItemTemplate>
<asp:Label runat="server" Text="<%# Item.IsEditable %>" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CheckBoxEditable " runat="server" Text="Editable"></asp:CheckBox>
</EditItemTemplate>
</asp:TemplateField>
When I click on edit button in the row, I would like that checkBox is already checked if value is true. (IsEditable is a boolean)
Text Field is easy because I have a BindItem on Text property in EditItemTemplate. But it's not the same for checkBox or dropdownlist
GridView
I use a UpdateItem Method to update my data in database. I tried a small condition to check my checkBox but it does'nt work.
public void GridViewRisquesAggravants_UpdateItem(IndexViewModel item)
{
try
{
if (ModelState.IsValid)
{
CheckBox chbEdit = (CheckBox)GridView.Rows[this.GridView.EditIndex].FindControl("CheckBoxEditable")
if (item.IsEditable)
chbEdit.Checked = true;
new TypeService().Update(new Type
{
IsEditable = item.IsEditable,
});
this.GridView.DataBind();
}
}
catch
{
throw;
}
}
It makes sense because I am not in the right function to declare this. But I just have 3 methods in my webform.
SelectMethod="GridView_GetData"
UpdateMethod="GridView_UpdateItem"
DeleteMethod="GridView_DeleteItem"
Where can I do this?
(And I have the same problem with datas on dropdownList. I don't know where I recover current value during editing)
Thanks in advance
(Sorry I am beginner about webforms and my english is not perfect)
Evy
use the following code instead for checkbox declaration in edit template
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean("true") %>' />
CheckBox chbx = GridView1.HeaderRow.FindControl("CheckBoxEditable") as CheckBox;
if (chbx != null && chbx.Checked)
{
//code here
}
else
{
//else condtion
}
hope this helps
Where can I do this?
Try it in RowDataBound event:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chbEdit = (CheckBox)e.Row.FindControl("CheckBoxEditable");
string value = ((Label)e.Row.FindControl("lblID")).Text;
if (value=="True")
chbEdit.Checked = true;
else
chbEdit.Checked = false;
}
}
Note: Don't forget to add OnRowDataBound in GrindView <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" >
I added the property Checked=<%# BindItem.IsEditable %> on CheckBox Control and it works perfectly.
I am working on an e-commerce app and the problem is in showing the quantity of items purchased. The DropDownList shows the option to change quantity, it is in a gridview and it has a selectedindexchanged event. The event code works fine but when there are two(or more) products and we change the quantity of one row and then change the other rows's quantity, the previous row quantity is set back to default that is "1" while total price column shows the multiplication of price and quanity that we set to the DropDownList.
My DropDownList code:
<asp:TemplateField HeaderText="Qty">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="ddlQty" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlQty_SelectedIndexChanged">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
My C# code for the SelectedIndexChanged and GridView RowDataBoundevent:
protected void ddlQty_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlQty =(DropDownList)sender;
int rowindex = int.Parse(ddlQty.Attributes["rowindex"].ToString());
ViewState["index"] = rowindex;
ViewState["I"] = ddlQty.SelectedValue;
decimal price = Convert.ToDecimal(ds.Tables[0].Rows[rowindex]["Price"].ToString());
decimal totalprice = price * Convert.ToInt32(ddlQty.SelectedValue);
ds.Tables[0].Rows[rowindex]["TotalPrice"] = totalprice;
GridView1.DataSource = ds;
GridView1.DataBind();
ds.Tables[1].Rows[0]["TotalPrice"] = decimal.Parse(ds.Tables[1].Rows[0]["TotalPrice"].ToString()) + totalprice - Convert.ToDecimal(ds.Tables[0].Rows[rowindex]["Price"].ToString());
GridView1.FooterRow.Cells[2].Text = "Total Amount =";
GridView1.FooterRow.Cells[3].Text = ds.Tables[1].Rows[0]["TotalPrice"].ToString();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlQty");
ddl.Attributes.Add("rowindex", e.Row.RowIndex.ToString());
if (ViewState["index"] != null)
{
if (e.Row.RowIndex.ToString() == ViewState["index"].ToString())
{
ddl.SelectedValue = ViewState["I"].ToString();
}
}
}
}
I am attaching an image also to make it more clear.
I think your grid is binding on each postback made by dropdownlist, so changing one value will overwrite the formerly stored value in VS. You should try storing these values as a list, appending changes to it instead of replacing it each time.
IMHO, anyway, I would go by setting AutoPostBack properties to false, and updating row calculations client-side, then validating on server side on submit.
I have a gridview with the following columns:
NAME|AGE|Birthday|Code
Joh 21 12.12.2 Yes/No
Currently the column code is a textbox. How can I transform it into a dropdownlist with 2 values: Yes/No so if I press edit I can choose in that cell the Value Yes or No.
Also How can I check on edit event too see if value is yes?
You can create a template field like this:
<columns>
<asp:TemplateField HeaderText="code">
<ItemTemplate>
<asp:DropDownList ID ="ddlCode" runat="server" AppendDataBoundItems="true" CssClass="DropDn1" />
</ItemTemplate>
</asp:TemplateField>
</columns>
In your rowdatabound event of the grid you will bind it if you want to bind from the database.
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList code= (DropDownList)e.Row.FindControl("ddlCode") as DropDownList;
if (code!= null)
{
//Bind the dropdownlist
}
}
To retrieve the value from the dropdown in your edit event you will do this:
string code = (row.FindControl("ddlCode") as DropDownList).Text);
I am using VS2005 C#.
Currently I have a GridView, and I have changed one of my GridView control to my column name Gender, from the default TextBox to a DropDownList, which I gave the ID of the control to GenderList, and it contains 2 values, M and F.
I have a default update statement which is able to update the GridView after edit, which is the following:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString=
"<%$ ConnectionStrings:SODConnectionString %>" UpdateCommand="UPDATE
[UserMasterData] SET [Name] = #Name, [Age] = #Age, [ContactNo]=#ContactNo,
[Address]=#Address, [Gender]=#Gender"/>
The above UPDATE query works perfectly, and now I have changed my Gender textbox to a dropdownlist, the UPDATE query gave me an error which says:
Must declare the scalar variable "#Gender".
I assume the UPDATE query couldn't find the value from my Gender column.
I tried to modify the UPDATE query to #GenderList, but it did not work as well.
Anyone knows what I should do do the UPDATE query so that my UPDATE query can find the value from my GenderList dropdownlist in my Gender column?
Thank you.
Below is my previous Gender column with a textbox control:
<asp:BoundField HeaderText="Gender"
DataField="Gender"
SortExpression="Gender"></asp:BoundField>
Below is my Gender with the dropdownlist control:
<asp:TemplateField HeaderText="Gender" SortExpression="Gender" >
<EditItemTemplate>
<asp:DropDownList ID="GenderList" runat="server" Width="50px" >
<asp:ListItem>M</asp:ListItem>
<asp:ListItem>F</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
EDIT:
Tried implementing RowDatBound and onRowUpdating:
RowDatBound
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView dRowView = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList genderList= (DropDownList)e.Row.FindControl("GenderList");
genderList.SelectedValue = dRowView[2].ToString();
}
}
}
RowUpdating.aspx.cs
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DropDownList genderSelect =(DropDownList)GridView1.Rows[e.RowIndex].FindControl("GenderList");
SqlDataSource1.UpdateParameters["Gender"].DefaultValue =
genderSelect.SelectedValue; --> error says not set to an instance of an object
}
If you are using SqlDataSource and updating data via it then all you have to do is to set 2 way binding for the dropdownlist GenderList.
You can set this via the designer or directly in source also
<EditItemTemplate>
<asp:DropDownList ID="GenderList" runat="server"
SelectedValue='<%# Bind("Gender") %>'>
<asp:ListItem>M</asp:ListItem>
<asp:ListItem>F</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
notice that here 2 way binding is being used.
you need to use its selected value property, dropdown list is collection of values, but you are assigning a scalar value to your update statements.
You have to check when its in update mode, you need to get selected item value of dropdown list and use that value as parameter for update statements.
Editing with Dropdownlist in gridview
to check its in edit mode or not use like this in RowDataBound Event
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
I want to add a dropdownlist to every entry in a gridview.
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Bank">
<ItemTemplate>
<asp:DropDownList ID="DropDown"
AutoPostBack="true" runat="server" DataTextField="Name" DataValueField="Name"
>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
At the back end i have the following code in order to bind a datatable to that dropdown list.
DataTable reader = BusinessLayer.BusinessLayerHandler.GetBankList();
DropDown.DataSource = reader;
DropDown.DataTextField = "NAME";
DropDown.DataValueField = "NAME";
DropDown.DataBind();
My problem is that the drop down list created at the grid view (DropDown) is not found at the back end as if it doesn't exist..
What can I do?
The DropDownList will be created for every single item in the GridView, so there can't be one field for the dropdownlists. Nevertheless, you can retrieve the DropDownList for a single row (e.g. in RowDataBound or RowCreated event)
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(r.Row.RowType == DataControlRowType.DataRow)
{
DropDownList dropdown = e.Row.FindControl("DropDown") as DropDownList;
if(dropdown != null)
{ /* your code */ }
}
}
Or you can use an event of the DropDownList itself and access the sender parameter.
<asp:DropDownList ID="DropDown" OnLoad="dropdownLoad" />
protected void dropdownLoad(object sender, EventArgs e)
{
DropDownList dropdown = sender as DropDownList;
if(dropdown != null)
{ /* your code */ }
}
you can find dropdown into grid databound event by grid.findcontrol.