Gridview Edit external update button. - asp.net

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

Related

Adding Drop down inside gridview header

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>

Is it possible to make some of the bounded fields in a grid view invisible via CSS?

I have a standard grid view, which contains a number of bounded fields and template fields, out of these I would like to make 4 columns invisible.
I know the css for this is visibility:hidden; but I do not know how to apply it to just these 4 columns, I have tried using the ControlStyle-CssClass property but it seems to apply the css to the whole table.
Is there a way to apply this css only to these 4 columns, also the plan is after I manage this is to write some Javascript for a button to switch the visibility on and off, so if you could please use some CSS/tags etc which can be easily changed client side it would be a huge plus.
A example:
<asp:GridView ID="gwFoo" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
Width="631px" OnRowDataBound="gwFoo_RowDataBound"
CssClass="customerDataTable" AllowSorting="True">
<Columns>
<asp:BoundField DataField="type" HeaderText="Type" SortExpression="type" />
<asp:TemplateField HeaderText="Number" SortExpression="id">
<ItemTemplate>
<asp:HyperLink ID="hlNumber" runat="server">[hlNumber]</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="product" HeaderText="Product" SortExpression="product" />
<asp:BoundField DataField="startDate" HeaderText="Start Date" SortExpression="startDate" />
<asp:BoundField DataField="endDate" HeaderText="End Date" SortExpression="endDate" />
<asp:BoundField DataField="isActive" HeaderText="Is Active ?" SortExpression="isActive" />
<asp:BoundField DataField="markedForActivation" HeaderText="Marked for Activation ?" SortExpression="markedForActivation" />
<asp:BoundField DataField="status" HeaderText="Status" SortExpression="status" />
<asp:TemplateField HeaderText="Parent Order" SortExpression="Foo">
<ItemTemplate>
<asp:HyperLink ID="hlFoo" runat="server">[hlFoo]</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Parent Document" SortExpression="Foo">
<ItemTemplate>
<asp:HyperLink ID="hlFoo" runat="server">[hlFoo]</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="createdDate" HeaderText="Created Date" <itemstyle Visible="false"></itemstyle> SortExpression="createdDate" />
<asp:BoundField DataField="createdBy" HeaderText="Created By" SortExpression="createdBy" />
<asp:BoundField DataField="lastUpdated" HeaderText="Last Updated" SortExpression="lastUpdated" />
<asp:BoundField DataField="lastUpdatedBy" HeaderText="Last Updated By" SortExpression="lastUpdatedBy" />
</Columns>
</asp:GridView>
give the columns a class and set the class css to visiblity:hidden;
Using Visible="false" will works
ie
<asp:BoundField Visible="false" DataField="status" HeaderText="Status" SortExpression="status" />
I solved the problem, in a way in which I can also switch the invissibility on and off from a button.
First I refactored the bounded fields I wanted to hide in this manner:
<asp:BoundField DataField="createdBy" HeaderText="Created By" SortExpression="createdBy" >
<ItemStyle CssClass="fooClass"></ItemStyle>
</asp:BoundField>
The class fooClass is a empty css class, I used it only so that all the <td> </td> elements which are generated by the bounded field share a common class, thus allowing the jquery script I wrote bellow to hide or show them.
<script> $(".fooClass").hide(); </script>
// or
<script> $(".fooClass").show(); </script>

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.

keeping GridView checkbox state when posting back

I have a GridView which with a CheckBox in a TemplateField.
When I check something and hit the submit button, the items that I have checked are not check on the postback.
I tried using EnableViewState and that did not help.
How can this be fixed?
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="SELECT">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ICAO" HeaderText="ICAO" />
<asp:BoundField DataField="IATA" HeaderText="IATA" />
<asp:BoundField DataField="AIRPORT_NAME" HeaderText="AIRPORT NAME" />
<asp:BoundField DataField="CITY" HeaderText="CITY" />
<asp:BoundField DataField="COUNTRY" HeaderText="COUNTRY" />
<asp:BoundField DataField="REVISED_DATE" HeaderText="REVISED DATE" />
<asp:BoundField DataField="EMAIL_DATE" HeaderText="EMAIL DATE" />
</Columns>
</asp:GridView>
You need to override LoadViewState and SaveViewState methods to maintain the selection.
https://web.archive.org/web/20211020153240/https://www.4guysfromrolla.com/articles/110205-1.aspx#postadlink

how to bind the data in gridview amount format 123,456.00?

I have a problem when binding the data in gridview. I am attempting to display the amount field in USA format like 123,456.00. How would I go about this with data binding?
The code I have so far is:
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:Label ID="lblAmount" runat="server" Text='<%# Eval("Amount") %>'>
</asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="right" VerticalAlign="Middle" />
</asp:TemplateField>
you can do this by dataformat string or you need any more help go through this link
format data string
<asp:BoundField DataField="amount" HeaderText="amount"
ReadOnly="True" SortExpression="amount" DataFormatString="{0:n3}" />
<asp:boundcolumn datafield="Price" headertext="Tax" dataformatstring="{0:C}">

Resources