How do I set the selected value in a gridview dropdownlist to what is in the current record like I currently do for a text box.
I am hoping the user can look at the current values and decide if they need to be changed -- this is what I use for a textbox:
<asp:TemplateField HeaderText="Other Notes" SortExpression="Other Notes" HeaderStyle-Wrap="false">
<edititemtemplate>
<asp:TextBox ID="txtOtherNotes" runat="server" Text='<%# Eval("[Other Notes]") %>' DataTextField="Other Notes" DataValueField="Other Notes"></asp:TextBox>
</edititemtemplate>
<itemtemplate>
<asp:Label runat="server" Text='<%# Bind("[Other Notes]") %>' ID="lblOtherNotes"></asp:Label>
</itemtemplate>
</asp:TemplateField>
You will need to use the OnRowDataBound event for that.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//check if the row is in edit mode
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
//find the dropdownlist in the row using findcontrol
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList");
//fill the dropdownlist from code behind if needed
ddl.DataSource = source;
ddl.DataTextField = "key";
ddl.DataValueField = "valee";
ddl.DataBind();
//cast the dataitem back to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
//set the correct listitem as selected
ddl.SelectedValue = row["myValue"].ToString();
}
}
}
I ended up using:
SelectedValue='<%# Bind("myID") %>'
to display the current record in the ddl.
Related
I have a gridview that displays data coming from an SQL data source (stored procedure) and I want to add a checkboxex column to it, here is my code:
TemplateField field = new TemplateField();
field.HeaderText = "Exporter ?";
gv.Columns.Add(field);
CheckBox cb = new CheckBox();
cb.Visible = true;
The problem is that I don't know how to add a checkbox to the TemplateField that I added to my gridview columns.
1) Add below code to GridView columns.
<asp:TemplateField HeaderText="CheckBoxColumn" Visible="False">
<ItemTemplate>
<asp:CheckBox ID="checkBox" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
2) Make checkbox column visible dynamically by adding OnRowDataBound event or just looping through GridView.Rows
int indexOfCBColumn = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[indexOfCBColumn].Visible = true;
}
foreach(GridViewRow row in GridView1.Rows) {
if(row.RowType == DataControlRowType.DataRow) {
row.Cells[indexOfCBColumn].Visible = true;
}
}
Thanks!!
You can add the code below into the GridView Columns.
<asp:templatefield HeaderText="Check Box">
<itemtemplate>
<asp:checkbox ID="cb" runat="server"></asp:checkbox>
</itemtemplate>
</asp:templatefield>
I want to make a column in the GridView a drop down list so the user can select an option from this list.
GridView code:
<asp:GridView style="float:left"
ID="gvBookings"
ShowHeaderWhenEmpty="true"
CssClass="tblResults"
runat="server"
OnRowDataBound="gvBooking_RowDataBound"
DataKeyField="ID"
AutoGenerateColumns="false"
allowpaging="false" />
<Columns>
<asp:BoundField DataField="FinishTime" HeaderText="Finish Time"></asp:BoundField>
<asp:BoundField DataField="TimeSpentName" HeaderText="Time Spent By"></asp:BoundField>
</Columns>
</asp:GridView>
Code Behind:
protected void gvBooking_RowDataBound(object sender, GridViewRowEventArgs e)
{
BHTaskClass.BookingTask booking = (BHTaskClass.BookingTask)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
int count = 1;
foreach (TableCell c in e.Row.Cells)
{
if (count == 1)
{
string FinishTime = booking.FinishTime.HasValue ? booking.FinishTime.Value.ToString("hh':'mm") : "";
c.Text = "<input type=\"text\" id=\"txtFinishTime" + booking.ID + "\" style=\"width:70px\" type=\"text\" onblur=\"UpdateFinishTime(" + booking.ID + ",this.value)\" value=\"" + FinishTime + "\" >";
}
count++;
}
}
}
In the code behind I changed the cell for FinishTime to a textbox. And when the user enters a value in here it calls a function that updates the database.
How do I change the cell to become a drop down menu? Can I do it did in the code behind like I created the textbox or would it be better to change the Boundfield in the GridView
I think you have to start using TemplateField instead of BoundField. Then you can put a TextBox and/or DropDownList directly in the GridView. You can then save the changes with the OnRowCommand event.
<asp:GridView ID="gvBookings"
runat="server"
OnRowDataBound="gvBookings_RowDataBound"
OnRowCommand="gvBookings_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1"
runat="server"
Text='<%# Eval("FinishTime") %>'></asp:TextBox>
<asp:LinkButton ID="LinkButton1"
runat="server"
CommandName="saveTextBox"
CommandArgument='<%# Container.DataItemIndex %>'>Save</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1"
runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind
protected void gvBookings_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//find the dropdownlist in the current row with findcontrol and cast back to one
DropDownList dropDownList = e.Row.FindControl("DropDownList1") as DropDownList;
//add some listitems
dropDownList.Items.Insert(0, new ListItem("Text A", "A"));
dropDownList.Items.Insert(1, new ListItem("Text B", "B"));
dropDownList.Items.Insert(2, new ListItem("Text C", "C"));
//select a value in the dropdown
dropDownList.SelectedValue = "B";
}
}
protected void gvBookings_RowCommand(object sender, GridViewCommandEventArgs e)
{
//check the commandname
if (e.CommandName == "saveTextBox")
{
//convert the commandargument to a row number
int rowNumber = Convert.ToInt32(e.CommandArgument);
//find the textbox in the current row with findcontrol and cast back to one
TextBox textBox = gvBookings.Rows[rowNumber].FindControl("TextBox1") as TextBox;
//do stuff with the textbox value
Label1.Text = textBox.Text;
}
}
How can I change the value of a textbox whenever a dropdownlist within a gridview has its value changed?
On page load, the textbox shows the selected value, but when I change the selection of the dropdownlist, the textbox value doesn't change.
The code is below.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false">
<Columns>
<asp:TemplateField HeaderText="Entry">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Duty">
<ItemTemplate>
<asp:DropDownList ID="duty" runat="server" OnLoad = "ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged" autopostback="true" EnableViewState="true"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The code behind is below.
protected void ddl1_load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
Duty dy = new Duty();
dt = dy.getdutyid(Convert.ToInt32(dropcontractid.SelectedValue));
DropDownList ddl = (DropDownList)sender;
ddl.DataSource = dt;
ddl.DataTextField = "dutyid";
ddl.DataValueField = "dutyid";
ddl.DataBind();
TextBox1.Text = ddl.SelectedValue;
}
}
You need to use SelectedIndexChanged handler to show selected value:
Markup:
<asp:DropDownList ID="duty" runat="server" OnLoad="ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged"></asp:DropDownList>
Code-behind:
protected void duty_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);
DropDownList duty= (DropDownList) gvr.FindControl("duty");
TextBox1.Text = duty.SelectedItem.Value;
}
I had a similar problem using the DropDownLists in GridView. My solution was to adjust the onLoad for the dropdown so that it wouldn't re-write the DropDownList on every post back. This way if there's something there then it won't re-populate it.
protected void dropDownLoad(object sender, EventArgs e)
{
DropDownList dropDown = sender as DropDownList;
if (dropDown.SelectedValue == null || dropDown.SelectedValue == "")
{
// Your Code to populate table
}
}
You should look into using data binding instead. You can bind the textbox.Text to the selecteditem.value, this will ensure that proper updating takes place
this happens to me once then i code like this... but i didnt use the onLoad attribute, tell me if this works,
<asp:TemplateField HeaderText="duty" SortExpression="duty">
<EditItemTemplate>
<asp:TextBox ID="duty" runat="server" Text='<%# Bind("duty_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblduty" runat="server" Text='<%# Eval("duty_Name") %>' />
<asp:DropDownList ID="ddlduty" runat="server" CssClass="dropdownlist"
OnLoad = "ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged" Visible = "false"
>
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle Width="5%" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
I want to set datatable header column fields to a Label controls Text property of a Gridview HeaderTemplate
I'm not sure I understand your question, but if you want to bind from a datasource to Labels within a TemplateField's HeaderTemplate and ItemTemplate, you can do this:
<asp:TemplateField HeaderText="YourField">
<HeaderTemplate>
<asp:Label runat="server" Text='<%# Eval("header_database_field") %>'/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("content_database_field") %>'/>
</ItemTemplate>
</asp:TemplateField>
Where header_database_field and content_database_field are the fields from your database that you wish to bind.
If you want to assign a DataTable's column name(s) to one of the TemplateField's Labels, you could do something like this programmatically during the RowDataBound event, though I'm not sure why you'd want to:
DataTable datatable = new DataTable(); // your dt
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
((Label)e.Row.Cells[0].Controls[1]).Text = datatable.Columns[0].ColumnName;
}
}
Reaplce datatable with your DataTable, and modify the indexes to reference your Cell/Label(s)/ColumnNames.
I have a gridview that has dropdownlist in edit section, I want to bind the selectedvalue from database when editing. In designer section there is no SelectedValue attribute, it gives runtime error. What to do any help?? Is there any way to handle it from code-behind?
<asp:TemplateField HeaderText="Company">
<EditItemTemplate>
<asp:DropDownList ID="DDLCompany" runat="server" DataValueField="cname" DataTextField="cname" SelectedValue = '<%# Bind("cname") %>' >
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="CompanyLabel" runat="server" Text='<%# Bind("cname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList DDLCompany = (DropDownList)e.Row.FindControl("DDLCompany");
DropDownList DDLPrinter = (DropDownList)e.Row.FindControl("DDLPrinter");
if (DDLCompany != null)
{
DDLCompany.DataSource = userobj.FetchCompanyList();
DDLCompany.DataBind();
DDLCompany.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString();
}
if (DDLPrinter != null)
{
DDLPrinter.DataSource = userobj.FetchPrinterList();
DDLPrinter.DataBind();
DDLPrinter.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString();
}
}
}
In Codebehind you have to check
for e.Row.RowType==
DataControlRowType.DataRow &&
e.Row.RowState ==
DataControlRowState.Edit in
RowDataBound before you find your
Dropdwonlist.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.dataitem.aspx
On aspx you can set the
seletcedvalue in the following way:
http://msdn.microsoft.com/en-us/library/ms178294.aspx