Display gridview cell values to textbox in asp.net - 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/

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>

Hide or insert tbody and tr tag in hidden gridview?

I'm trying to validate a page on our ASP.Net site that the developers used a gridview on. It seems that initially they are using a "placeholder" gridview that is getting hidden or visible based on some buttons being clicked to show data. Before these button clicks however, I'm seeing an empty table and the validation tool I'm using is complaining about no tbody or tr tags. Is there a way to completely hide the table tag or alternately to insert a tbody/tr within the hidden gridview?
Here is the gridview tag in the aspx.vb file:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" summery="This table displays system notification items."
AllowPaging="True" PagerSettings-Position="Top" PagerStyle-HorizontalAlign ="Right" PageSize ="15" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="image1" runat="server" ImageUrl="~/cmsicons/flag_red.gif" AlternateText="Overdue" Visible='<%# Eval("IsVisible")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataFormatString="{0:MM/dd/yyyy}" DataField="showdate" HeaderText="Due Date"
SortExpression="showdate" />
<asp:BoundField DataFormatString="{0:MM/dd/yyyy}" DataField="ModifiedDate" HeaderText="ModifiedDate"
SortExpression="ModifiedDate" />
<asp:TemplateField HeaderText="Data ID" SortExpression="DataRecordID">
<ItemTemplate>
<asp:HyperLink text='<%# Eval("DataRecordID") %>' runat="server" NavigateUrl='<%# Eval("DataRecordID", "CMSManageAllDataRecord.aspx?dataid={0}") %>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RecordName" HeaderText="Title"
SortExpression="RecordName" />
<asp:BoundField DataField="subcdrl" HeaderText="CDRL Number"
SortExpression="subcdrl" />
<asp:BoundField DataField="subcdrl" HeaderText="Sub Cdrl Count"
SortExpression="subcdrl" Visible ="false" />
<asp:BoundField DataField="StatusName" HeaderText="Status"
SortExpression="StatusName" />
</Columns>
<EmptyDataTemplate>
<div>No Data found</div>
</EmptyDataTemplate>
<FooterStyle CssClass="GridViewFooterStyle" />
<RowStyle CssClass="GridViewRowStyle" />
<SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
<AlternatingRowStyle CssClass ="GridViewAlternatingRowStyle" />
<PagerStyle CssClass="GridViewPagerStyle" HorizontalAlign="Right" />
<HeaderStyle CssClass="GridViewHeaderStyle" />
</asp:GridView>
And here is the output I see when I view source:
<div class="AspNet-GridView" id="ctl00_ContentPlaceHolder1_GridView1">
<table cellpadding="0" cellspacing="0" summary="">
</table>
</div>
You can definitely hide it with Javascript, if that's an option, one way would be
<script>
function hideGV(){
document.getElementById('<%=GridView1.ClientID%>').style.display='none';
}
</script>
If rather want to insert the tbody/tr elements and using jQuery is an option for you, here's an example.
jQuery option to insert tbody/tr
<script>
​$(function(){
$('#<%=GridView1.ClientID%>').append('<tbody><tr><td></td></tr></tbody>');
});​
</script>

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.

Gridview heading coming too large with one row

I'm using a gridview in asp.net. The gridview display heading is too large when it has one row.
But if it has pagination, it displays heading normally. I don't have any idea why.
Any help would be appreciated.
<asp:Panel ID="Panel2" runat="server" Height="310px" Width="100%" CssClass="mPanel" BorderStyle ="Groove">
<asp:GridView ID="gridViewResults" runat="server" Width = "100%"
AllowPaging = "true" OnPageIndexChanging="gridViewResults_PageIndexChanging"
CssClass="mGrid" OnSelectedIndexChanged="gridViewResults_SelectedIndexChanged"
Height="300px">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="chkSelect_CheckedChanged" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
this is my code and iam binding it thru databasee..
First try this
<HeaderStyle Width="10%" />
<RowStyle Width="10%" />
If not work than do it from code side for all your colums after binding the gridview
int cou = gdv.Columns.Count;
for (int i = 0; i < cou; i++)
{
gdvProReq.Columns[i].HeaderStyle.Width = 50;
gdvProReq.Columns[i].ItemStyle.Width = 50;
}
The GridView will by default automatically size its columns to fit the largest row... when you paginate, the large row that was stretching out the headers was probably moved to one of the later pages.
You can manually size the columns to avoid the headers from dynamically resizing:
<Columns>
<asp:BoundField DataField="" HeaderText="" ItemStyle-Width="5%" />
<asp:BoundField DataField="" HeaderText="" ItemStyle-Width="25%" />
<asp:BoundField DataField="" HeaderText="" ItemStyle-Width="30%" />
<asp:BoundField DataField="" HeaderText="" ItemStyle-Width="25%" />
<asp:BoundField DataField="" HeaderText="" ItemStyle-Width="10%" />
</Columns>

Resources