Adding Drop down inside gridview header - asp.net

I am new to ASP.net and trying to add dropdownlist and search text boxes under my header fields in Gridview control.
<asp:GridView ID="EmpGridView" runat="server" AutoGenerateColumns="false"
DataKeyNames="EMPLOYEEID"
AllowSorting="True" AllowPaging="true" PageSize="50"
OnPageIndexChanging="EmpGridView_PageIndexChanging" style="margin-right: 52px" OnSelectedIndexChanged="EmpGridView_SelectedIndexChanged"
>
<Columns>
<asp:BoundField DataField="EMPLOYEEID"
HeaderText="Employee ID" ReadOnly="true"
SortExpression="EMPLOYEEID" />
<asp:BoundField DataField="PERSONNAME"
HeaderText="Person Name" ReadOnly="true"
SortExpression="PERSONNAME" />
<asp:BoundField DataField="DIVISIONNAME"
HeaderText="Division Name" ReadOnly="true"
SortExpression="DIVISIONNAME" />
<asp:BoundField DataField="DESIGNATION"
HeaderText="Designation" ReadOnly="true"
SortExpression="DESIGNATION"
/>
<asp:BoundField DataField="CNIC"
HeaderText="CNIC" ReadOnly="true"
SortExpression="CNIC" />
</Columns>
</asp:GridView>
I want following
EMPLOYEEID PERSONNAME DIVISIONNAME <----HeaderText
TextBox control TextBox Control DropDownlist control <------aspcontrols
..data ..data ..data <------ rest is db
..data ..data ..data
Meaning I want my labels there too with my asp.net controls
How should I do it?
So far I tried following but could not work out where to place it? since if I add it separately then there are rows of each control which I don't want and boundfield tag does not permit it in it.
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="searchBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>

You need to use HeaderTemplate of the TemplateField for placing the controls on the header
<asp:TemplateField SortExpression="PERSONNAME">
<HeaderTemplate>
<asp:Literal runat="server">Person Name</asp:Literal>
<asp:TextBox runat="server" ID="searchBox"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Literal runat="server" Text='<%# Bind("PERSONNAME") %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>

Related

Is it possible to change the input type of a parameter inside a SqlDataSource?

I created a table using a Sqldatasource in .net webforms. I added a "commandField" that allows you to edit and update the content of this table, but by default the input type is "text". I would like to have different input types for some columns. For example the description column should be a "textarea" and the category column should be a "select dropdown".
I tried editing the input type= text in CSS, but this was a failure. Apparently only the textarea input type allows multiple lines to be displayed for the user.
This is how I'm calling the edit column in the table:
<asp:CommandField ButtonType="Button" ControlStyle-BackColor="DarkOrange" ControlStyle-CssClass="trCBPad" ItemStyle-CssClass="flex-container2" HeaderText="Edit" ShowEditButton="True">
<ControlStyle BackColor="DarkOrange" CssClass="trCBPad"></ControlStyle>
This is the dataField I would like to edit, so that the input is a textarea:
<asp:BoundField DataField="ProofPointId" HeaderText="ProofPointId" InsertVisible="False" ReadOnly="True" SortExpression="ProofPointId" />
If yor are using a gridview
<asp:GridView ID="GridView1" DataSourceId="MyDataSource" DataKeyNames="Code"
AutoGenerateColumns="false" AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true" runat="server">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%#Eval("Name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" Text='<%# Bind("Name")%>' runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<%#Eval("Description")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox TextMode="Multiline" ID="txtDesctiption"Text='<%# Bind("Description")%>'
runat="server" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Display gridview cell values to textbox in asp.net

I'm working with asp.net nested gridview,in which I need to show the gridview cell values to the textbox for editing.
My problem is that I don't know how to display values to textbox if i ve used both template field & boundfield. Here is my aspx.
<Columns>
<asp:TemplateField ItemStyle-Width="10px">
<ItemTemplate>
<img alt = "" style="cursor: pointer" src="images/plus.png" />
<asp:Panel ID="pnsections" runat="server" Style="display: none;">
<asp:HiddenField ID="HdnId" runat="server" Value='<%# Eval("Dept_Name") %>' />
<asp:GridView ID="gvsections" Width="100%" CssClass="table table-bordered table-hover" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvsections_rowdatabound" OnRowEditing="gvsections_rowediting" DataKeyNames="Dept_Name">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="currdept" HeaderText="Deptcurrent" />
<asp:BoundField ItemStyle-Width="150px" DataField="Dept_Name" HeaderText="SectionName" />
<asp:BoundField ItemStyle-Width="150px" DataField="FLDTYPE" HeaderText="Type" HeaderStyle-CssClass=" visible-lg visible-md" ItemStyle-CssClass=" visible-lg visible-md " />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="Dept_Name" HeaderText="Department" />
<asp:BoundField ItemStyle-Width="150px" DataField="FLDTYPE" HeaderText="Type" />
</Columns>
</asp:GridView>
if you are writing the code in selectedindexchanging event of the grid view then try this and it will work
GridViewRow row = gvdepts.Rows[e.NewSelectedIndex];
deptname.Text=row.Cells[1].Text;
I hope this helps
In your code 3 fields contain dept name -- HdnId(Hiddenfield),inner gridview bound field (Dept_Name),Outer gridview bound field(Dept_Name).
1. deptname.Text = (gvdepts.SelectedRow.Cells[1].FindControl("HdnId") as HiddenField).Value;
2. deptname.Text = (gvdepts.SelectedRow.Cells[1].FindControl("gvsections") as GridView).Rows[0].Cells[2].Text;
3. deptname.Text = gvdepts.SelectedRow.Cells[2].Text;
may this link solve your problem:
http://www.aspforums.net/Threads/133072/Edit-Update-Delete-in-Nested-Child-GridView-in-ASPNet/

Pass parameters withing Datasource

I need help passing a parameter to the DataSource BindCompanyCharItems() to filter records, which I bind to a CheckList called chklstCompCharItems.
The parameter I am trying to use is <asp:BoundField DataField="Id" HeaderText="CompCharID" />
My GridView:
<asp:GridView ID="grid_Data" runat="server" Width="100%" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="CompCharID" />
<asp:BoundField DataField="Name" HeaderText="Comp Char Name" />
<asp:TemplateField HeaderText="Invite Permission">
<ItemTemplate>
<asp:CheckBoxList ID="chklstCompCharItems" DataSource='<%# BindCompanyCharItems()%>' DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind ("Id") %>' runat="server">
</asp:CheckBoxList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Try following code:
DataSource='<%# BindCompanyCharItems((Eval("Id")))%>'
Your "BindCompanyCharItems()" must accept parameter and you should then use this parameter to filter.

Gridview Edit external update button.

I have a gridview in my asp.net page that has one template field for editing a yearly budget. The code follows:
<Columns>
<asp:BoundField DataField="f_year" HeaderText="Year" ReadOnly="True" `enter code here`>
<ItemStyle Width="35px" />
</asp:BoundField>
<asp:BoundField DataField="o_orgcode" HeaderText="Cost Center " ReadOnly="True" />
<asp:BoundField DataField="o_orgdesc" HeaderText="CostCenterDesc " ReadOnly="True" />
<asp:BoundField DataField="s_subobject" HeaderText="Account " ReadOnly="True" />
<asp:BoundField DataField="s_subdescrip" HeaderText="AccountDesc" ReadOnly="True" />
<asp:TemplateField HeaderText="Initial Appropriation">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text="$"></asp:Label>
<asp:TextBox ID="txtEditItemApprAmt" runat="server" Text='<%# Bind("b_budgetamt", "{0:f}") %>' CssClass="boxright"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txtEditItemApprAmt2" runat="server" Text='<%# Bind("b_budgetamt", "{0:f}") %>' CssClass="boxright"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:BoundField DataField="b_initials" ReadOnly="True" >
<ItemStyle Width="35px" />
</asp:BoundField>
When my page loads the one template I field I have will automatically appear as a databound text box. I do this because there are multiple money figures to enter at one time and I believe this to be a better function for me site. I want to be able to click an external (meaning external to the gridview) button to loop through the gridview and change those values in my database. Anyone know how to do this, I have not been able to find on the internet an example of this?? Thanks!
You can do it this way,
foreach(GridVieRrow row in gv.rows)
{
TextBox txtEditItemApprAmt = row.FindControl("txtEditItemApprAmt");
string ApprAmt = txtEditItemApprAmt.Text;
txtEditItemApprAmt.Text = someTextFunctionOrVariable;
}

Could not Update Text box control values

I am really frustrated to find a solution for my scenario. I have two gird when First grid is used show the shop information and the second grid is used to edit the offers related to the shop information. When the users edit the second grid i will just update the text box values related to the grid selection row. in my code behind file i can see the data fetching from the grid and assigning into the text boxes but when the function call (imgEdit_click) finished the page does not show the values. Editing functionality can be done in many ways but my scenario is what i explained earlier . I have the checked page there is no Postback action has been called after the method I could not find the solution can anyone help me to figure it out.
Following are my source and code behind codes.
My design Source :
<div class="field">
<asp:TextBox ID="txtareaOfferDesc" runat="server" TextMode="MultiLine" ></asp:TextBox>
</div>
<div class="field">
<asp:TextBox ID="txtTimeStarts" runat="server" CssClass="textfield"></asp:TextBox>
<cc1:CalendarExtender
ID="CalendarExtender2"
runat="server"
TargetControlID="txtTimeStarts"
CssClass="CalendarCSS">
</cc1:CalendarExtender>
<div class="datefld">
<label class="name">Offer end date/time (optional)</label>
<div class="field">
<asp:TextBox ID="txtTimeEnd" runat="server" CssClass="textfield"></asp:TextBox>
<cc1:CalendarExtender ID="CalendarExtender4" runat="server" TargetControlID="txtTimeEnd"
CssClass="CalendarCSS">
</cc1:CalendarExtender> `
<asp:GridView ID="gvShopDeal" runat="server" AutoGenerateColumns="false"
CssClass="tblexistoffer" DataKeyNames="ShopID" AllowPaging="True"
AllowSorting="True">
<Columns>
<asp:TemplateField>
<HeaderTemplate >
<asp:CheckBox ID="chkHeader" runat="server" />
<asp:Label ID="lblSelectAll" Text="Select All" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate >
<asp:CheckBox ID="chkRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ShopID" HeaderStyle-Width="10%" HeaderText="Shop ID" />
<asp:BoundField DataField="ShopName" HeaderStyle-Width="40%" HeaderText="Shop Name" />
<asp:BoundField DataField="Street" HeaderStyle-Width="40%" HeaderText="Street" />
<asp:BoundField DataField="City" HeaderText ="City" />
</Columns>
</asp:GridView>
<asp:Button ID="btnCreateDeal" runat="server" Text="Create Offer"
CssClass="grnbtn" OnClientClick="return CheckDealValidation(this)" onclick="btnCreateDeal_Click"></asp:Button>
<asp:Button ID="btnDefCancel" runat="server" Text="Cancel" CssClass="greybtn"></asp:Button>
<asp:UpdatePanel ID="UpdateExistingOffer" runat="server">
<ContentTemplate>
<asp:GridView ID="gvExistingOffers" runat="server" CssClass="tblexistoffer"
AutoGenerateColumns="false" DataKeyNames="OfferID" AllowPaging="True"
AllowSorting="True">
<Columns>
<asp:BoundField DataField="OfferID" HeaderText="OfferID" />
<asp:BoundField DataField="Description" HeaderText="OfferName" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="OfferType" HeaderText="OfferType" />
<asp:BoundField DataField="StartDate" HeaderText="StartDate">
<ItemStyle CssClass="Hide" />
<HeaderStyle CssClass="Hide" />
</asp:BoundField>
<asp:BoundField DataField="EndDate" HeaderText="EndDate" >
<ItemStyle CssClass="Hide" />
<HeaderStyle CssClass="Hide" />
</asp:BoundField>
<asp:TemplateField HeaderText="Edit" HeaderStyle-Width="5%">
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" ImageUrl="~/Merchant/images/edit.jpg" runat="server" Width="25" Height="25" onclick="imgbtnEdit_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Code Behind File :
protected void imgbtnEdit_Click(object sender, ImageClickEventArgs e)
{
ImageButton btndetails = sender as ImageButton;
GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;
fferIDForShop = Convert.ToInt32(gvExistingOffers.DataKeys[gvrow.RowIndex].Value);
ShopList objShopID = ShopService.GetShopID(OfferIDForShop);
(txtareaOfferDesc.Text) = gvrow.Cells[1].Text.Trim();
txtTimeStarts.Text = gvrow.Cells[4].Text;
txtTimeEnd.Text = gvrow.Cells[5].Text;
}
Thanks
Vijay
Issue is because textboxes are out of Update Panel, So just put everything in update panel it will start functioning or just comment out update panel and then try the same thing.

Resources