Adding dropdown list to columns in gridview - c#

I have a grid and I want to set a dropdown list with Yes & No for some columns. I am not able to put that dropdown list like I mentioned below. Its because I use checkbox in the grid to edit the rows. So even if I put these I am not able to see the drop down when I click the checkbox to edit.
<asp:TemplateField HeaderText="Lead" ItemStyle-Width="100">
<ItemTemplate>
<asp:Label ID="lblLead" runat="server" Text='<%# Bind("Lead") %>'></asp:Label>
<asp:TextBox ID="txtLead" runat="server" Text='<%# Bind("Lead") %>' Visible="false"></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="100px" />
<EditItemTemplate>
<asp:DropDownList id="ddlLead" runat="server">
<asp:ListItem Value="Yes"> Yes </asp:ListItem>
<asp:ListItem Value="No"> No </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>

Try this
protected void grdVw_RowEditing(object sender, GridViewEditEventArgs e)
{
grdVw.EditIndex = e.NewEditIndex;
/* Insert specific editing logic here */
grdBind();//method to bind gridview
}

First you gotta make sure you are setting the mode to Edit. For this I use the default buttons, and you are using checkboxes, they should both work:
<asp:GridView ID="GridView1" runat="server" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:CommandField ShowEditButton="True" />
<%--Other columns here--%>
</Columns>
</asp:GridView>
Then, you need to handle the RowEditing event, by setting the EditIndex to the index of the row you want to edit, and re-bind the GridView:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
setDataSource(); //this sets the data source of the grid, and re-binds it.
}

Related

Event in Update panel is not fired after rebinding control after Postback

I have a Gridview together with some LinkButton inside of an Update Panel. The click events are fired correctly, the way I expect them to work. BUT: After adding an additional Header Row to the grid in code behind, I need to rebind the Grid after each postback. Since then the click causes a postback, but the method is not executed.
<asp:UpdatePanel runat="server" ID="UpdatePanel2" UpdateMode="Always">
<ContentTemplate>
<asp:GridView ID="GridViewFollowUpMove" runat="server" AutoGenerateColumns ="false" OnRowDataBound="FollowUp_RowDataBound" OnDataBound="FollowUpMove_OnDataBound">
<Columns>
<asp:TemplateField HeaderText="Due date" ItemStyle-HorizontalAlign="left">
<ItemTemplate>
<asp:LinkButton ID="LB_DueDate" runat="server" OnClick="ActivateDueDate" CssClass="NoDeco" CommandArgument='<%# Bind("ISSUEID") %>'>
<asp:Label ID="LabelDueDate" runat="server" Text='<%# Bind("DueDate","{0:dd.MM.yyyy}") %>' Width="60px" Class="line"/></asp:LinkButton>
<asp:TextBox ID="TXTBX_DueDate" runat="server" Text='<%# Bind("DueDate") %>' Font-Size="11px" visible="false" OnTextChanged="TextChanged_DueDate" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillFollowUp();
}
FillFollowUp();
}
I solved this problem by moving the commands that create and customize the additional Header row to the OnRowCreated Event. As I read somewhere: When a postback occurs, the Gridview must be recreated on the server, and in this process ItemCreated is fired for each item, but ItemDataBound is not (since the control properties are pulled from ViewState).
So building these headers in DataBound is the wrong choice!

In Gridview make a dropdownbox visible when a checkbox is selected

I have a gridview with 2 templatefields, one is a checkbox and the other is a dropdownlist. I want to be able to have the dropdownlist for that row visible if a user selects the checkbox.
<asp:TemplateField HeaderText="Follow-up Needed" ItemStyle-Width="75px">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:CheckBox ID="CBFollowUp" runat="server" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Follow-Up Assignment" ItemStyle-Width="100px">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:DropDownList ID="DropDownListFollowUpUser" runat="server" Visible="false"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
I tried to put the logic in the RowDataBound, but on a postback I lose the checkbox selection, but if I do !isPostBack on my Databind, then It won't fire RowDataBound when a checkbox is selected.
I'm not sure where and what logic I need to make to make this fluid, Do I need to do it in the OnCheckedChanged, or javascript?
Thank you for any feedback.
One option would be to use the OnCheckChanged event like you thought. First add it to your CheckBox.
<asp:CheckBox ID="CBFollowUp" runat="server"
AutoPostBack="true"
OnCheckChanged="CBFollowUp_CheckChanged" />
Then handle it in your codebehind. In here you would find your DropDownList that is within the same row as the CheckBox you just clicked and then hide/show it as necessary.
protected void CBFollowUp_CheckChanged(object sender, EventArgs e)
{
CheckBox CBFollowUp = (CheckBox)sender;
GridViewRow row = (GridViewRow)CBFollowUp.NamingContainer;
DropDownList DropDownListFollowUpUser = (DropDownList)row.FindControl("DropDownListFollowUpUser");
DropDownListFollowUpUser.Visible = CBFollowUp.Checked;
}

DataGrid fetching particular row on dropdownselectedindex value change event

Im working on one small project.Using Datagrid in that
<asp:DataGrid ID="dgShowTiming"
runat="server"
AutoGenerateColumns="false"
OnItemCreated="dgShowTiming_ItemCreated">
<Columns>
<asp:TemplateColumn HeaderText="HOUR">
<ItemTemplate>
<asp:DropDownList ID="ddlShowTimingsHours"
runat="server"
CssClass="field1"
DataSource="<%#Hour()%>"
DataTextField="Hours"
DataValueField="Hours">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="MINUTE">
<ItemTemplate>
<asp:DropDownList ID="ddlShowTimingsminutes"
runat="server"
CssClass="field1"
DataSource="<%#Minute()%>"
DataTextField="Minutes"
DataValueField="Minutes">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="AM/PM">
<ItemTemplate>
<asp:DropDownList ID="ddlShowTimingAMPM"
runat="server"
CssClass="field1"
onchange="GetCountryDetails()">
<asp:ListItem>AM</asp:ListItem>
<asp:ListItem>PM</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<ItemTemplate>
<asp:DropDownList ID="ddlShowTimingDescription"
runat="server"
DataSource="<%#Description()%>"
DataTextField="ShowTimeDesc"
DataValueField="ShowTimeDescID"
CssClass="field1">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Tax Details">
<ItemTemplate>
<asp:Label ID="lblRowID"
runat="Server"
Text="View"
Style="cursor: pointer;"
onclick="FilmTaxDetailsOpen(this);"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
So Now My problem in this Grid is i have to on selectedindexchange value of dropdown ddlShowTimingAMPM i want to change the description as NOON or morning or evening..Problem im facing is how to fetch the values of 3 dropdownlist selected , based on that only im changing the description.
try this 100% working and tested
Once you Find row then from that row you can find all Controls inside that row. You can same apply for other DropDownList and set AutoPostBack="true" for all DropDownList
protected void ddlShowTimingsHours_OnSelectedIndexChanged(object sender, EventArgs e)
{
DataGridItem item = (DataGridItem)((DropDownList)sender).Parent.Parent;
DropDownList ddlShowTimingsHours = (DropDownList)item.FindControl("ddlShowTimingsHours");
DropDownList ddlShowTimingsminutes= (DropDownList)item.FindControl("ddlShowTimingsminutes");
DropDownList ddlShowTimingAMPM= (DropDownList)item.FindControl("ddlShowTimingAMPM");
}

Execute Update on DropDownList change

At the moment, I have a GridView with the following in the ItemTemplate:
<asp:LinkButton ID="lbEdit" runat="server" CommandName="Edit">
<asp:Label ID="Label6x" runat="server" Text='<%# Bind("progress_full") %>' /></asp:LinkButton>
This works fine and when I click on it, it displays the EditTemplate which currently contains the following:
<asp:DropDownList ID="DropDownList3" runat="server"
SelectedValue='<%# Bind("progress") %>'>
<asp:ListItem Value="0">In queue</asp:ListItem>
<asp:ListItem Value="1">Being worked on</asp:ListItem>
<asp:ListItem Value="2">Complete</asp:ListItem>
</asp:DropDownList><br />
<asp:LinkButton ID="lbUpdate" runat="server" CommandName="Update" Text="Update" />
How can I get the dropdown to automatically execute the Update command when it is changed, instantly returning back to the ItemTemplate, instead of me making the change to the DropDown and having to click Update?
You add AutoPostBack="true" to your DropDownList and set OnSelecIndexChanged="DropDownList1_SelectedIndexChanged"
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//execute here your update
......
}

DropDownList event within Accordian and GridView

An event bound to a DropDownList in a standalone GridView obviously would work in this fashion , but things are bit more complicated in this scenario.
The event does not fire for the DropDownList. What's interesting is the event bound to the Button Does fire. Not sure what the difference would be between the DropDownList and TextBox.
I've tried both OnSelectedIndexChanged and OnTextChanged - neither work.
The nesting is as follows:
GridView A
Ajax Accordion
GridView B (With DropDownList)
<AjaxToolkit:AccordionPane ID="AccordionPane1" runat="server">
<Header>
</Header>
<Content>
<asp:GridView runat="server" ID="gv" AutoGenerateColumns="false"
BorderWidth="0" AlternatingRowStyle-BorderStyle="None" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label runat="server" ID="lblId" Text='<%# Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:Label runat="server" ID="lblType"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList runat="server" ID="ddlType" OnTextChanged="ddlType_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
<asp:Button runat="server" ID="btnTest" OnClick="btnTest_Click" Text="TEST" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</Content>
Thank you!
UPDATE
Turns out this had nothing to do with the nested GridViews or Accordion.
After adding the following, the event now successfully fires:
if (!Page.IsPostBack)
Populate(object);
Turns out this had nothing to do with the nested GridViews or Accordion.
After adding the following, the event now successfully fires:
if (!Page.IsPostBack)
Populate(obj);

Categories

Resources