Update GridView with Dropdownlist control - c#

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)

Related

Need to access text of record in dropdown index

I'm trying to build some handling for OnRowUpdating in my GridView gvTaskDashboard. I am wanting to check that if a Status dropdown list has Complete selected, that a date has been entered into the Complete Date textbox. I'm sure the problem is in my if statement trying to test for Complete, but because the dropdown is bound to the index of a sql table, I'm not sure how to check for the text value of the selected index. I'm sure I could get by with using if (ddlStatus.SelectedValue == "5") for example, but as a coder, I would think checking for "Complete" makes more sense from a maintenance standpoint.
Markup for DDL
<asp:DropDownList ID="ddlStatus" runat="server" AutoPostBack="True" DataSourceID="ddlStatusSQL"
DataTextField="TaskStatusName" DataValueField="TaskStatusID" SelectedValue='<%# Bind("TaskStatusID") %>'>
</asp:DropDownList>
SQL DataSource for DDL
<asp:SqlDataSource ID="ddlStatusSQL" runat="server" ConnectionString="<%$ ConnectionStrings:ProWorxConnectionString %>"
SelectCommand="SELECT [TaskStatusID], [TaskStatusName] FROM [tblTaskStatus]
ORDER BY [TaskStatusType], [TaskStatusName]">
</asp:SqlDataSource>
CodeBehind
protected void gvTask_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow gvr = gvTaskDashboard.Rows[e.RowIndex];
// Get Controls
DropDownList ddlStatus = (DropDownList)gvr.FindControl("ddlStatus");
TextBox txtCompDate = (TextBox)gvr.FindControl("txtCompDate");
// Test for Complete Date if Complete status selected
if (string.IsNullOrWhiteSpace(txtCompDate.Text) && ddlStatus.SelectedValue == "Complete")
{
// Throw Error
ClientScript.RegisterStartupScript(GetType(), "error", "alert('Enter Complete Date if Status is Complete.');", true);
}
else { TaskDashboardSQL.Update(); }
}
Hi give this a whirl:
ddlStatus.SelectedItem.Text == "Complete"

Nesting a ListView inside a GridView

I have an ASP.NET GridView which amongst other values binds an ID to one of the columns.
Another one of the columns of this table should contain a list of items, which should be resolved by passing in the ID from the GridView.
To achieve this, I tried nesting the ListView inside the GridView, and passing the ID into the Default Parameter of an ObjectDataSource used by the ListView, but this syntax is not allowed:
<asp:TemplateField HeaderText="columnItems">
<ItemTemplate>
<asp:ListView ID="listOfItems" runat="server" DataSourceID="MyObjectDataSource >
<ItemTemplate>
<asp:LinkButton ID="MyLinkButton" Runat="Server" Text='item'></asp:LinkButton>
</ItemTemplate>
</asp:ListView>
<asp:ObjectDataSource ID="MyObjectDataSource" runat="server"
TypeName="MyTypeName.Whatever" SelectMethod="GetItems">
<SelectParameters>
<asp:Parameter Name="requestId" Type="String" DefaultValue='<%# Eval("ID")'/>
</SelectParameters>
</asp:ObjectDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
So how do I go about passing in the ID so I can get the list of items?
You probably need to do that in the RowDataBound event, get the ID there and then do you DB
then do something like
if(e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
ListView a = (ListView)e.Row.FindControl("listOfItems");
a.datasource = // the result of your db call
a.databind();
Attach a 'OnRowDataBound' event to the GridView control to retrieve the items for the ListView for each row on the GridView (after the GridView has been bound):
e.g. On the ASPX page:
<asp:GridView id="MyGridView" OnRowDataBound="GetItems" ... > ... </asp:GridView>
In the code-behind:
protected void GetItems(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
// Get the ID from the GridView
var dataRowView = (DataRowView) e.Row.DataItem;
var id = dataRowView["ID"].ToString();
// Bind the supporting documents to the ListView control
var listView = (ListView) e.Row.FindControl("listOfItems");
listView.DataSource = /* Call to database to return a DataSet of items */;
listView.DataBind();
}
(As would be appropriate, I tried editing Tunisiano's post to elaborate on his answer on attaching the event to the GridView and getting the request ID, but SO editors are rejecting it for no good reason. The above code is tested and answers the question exactly).

Getting pre-selected Text in dropdown in EditItemTemplate gridview

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.

GridView checbox field is Grayed?

I have a GridView that draws fields from a dataset:
protected void Page_Load(object sender, EventArgs e)
{
var dat = SecurityManager.GetAllGroups();
GridView1.DataSource = dat;
GridView1.DataBind();
}
One of these is the IsAdministrator property.
It should show up as a checkbox.
It does, but it is grayed out. I want it to be enabled so I can modify it.
From there, I will go through each row and update accordingly.
Is this possible to do? How can I ungray the checkbox?
Thanks
If you are using the CheckBoxField, you can only change the value when in edit mode. To workaround this, if you want a checkbox for all rows, use the TemplateField, and supply a checkbox in the item template like:
<asp:TemplateField ..>
<ItemTemplate>
<asp:CheckBox .. Checked='<%# Bind("IsAdministrator") %>' />
</ItemTemplate>
</asp:TemplateField>

How to transform a column from a gridview in a dropdownlist with 2 values to choose

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);

Categories

Resources