retain visible property of control through paging of nested gridview - c#

I have a grid view within a grid view. When I click on edit of parent grid view the child grid view displays a checkbox. When I click on next page button of child grid view the controls that were made visible become invisible again. This is not the behaviour I require. I would like the controls that are made visible by clicking edit to remain visible through paging of the child grid view while parent grid view is in edit mode.
My markup:
<asp:GridView
ID="grdImages"
runat="server"
AllowPaging="true"
ShowFooter="true"
PageSize="5"
AutoGenerateColumns="false"
OnPageIndexChanging="grdImages_PageIndexChanging"
OnRowCancelingEdit="grdImages_RowCancelingEdit"
OnRowCommand="grdImages_RowCommand"
OnRowEditing="grdImages_RowEditing"
OnRowUpdating="grdImages_RowUpdating"
OnRowDeleting="grdImages_RowDeleting"
EmptyDataText="No Data Available at this Time"
OnRowDataBound="grdImages_RowDataBound"
DataKeyNames="productID" RowStyle-VerticalAlign="Top" RowStyle-HorizontalAlign="Center">
<AlternatingRowStyle BackColor="White" ForeColor="#284775"></AlternatingRowStyle>
<Columns>
<asp:TemplateField AccessibleHeaderText="Product ID" HeaderText="Product ID" FooterText="Product ID">
<ItemTemplate>
<asp:Label ID="lblProdId" runat="server" Text='<%# Eval("ProductId") %>' ></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="lstAddProdId" runat="server" AppendDataBoundItems="true" >
<asp:ListItem>Select a product</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Product Main Image" FooterText="Product Main Image" HeaderText="Product Main Image">
<ItemTemplate>
<asp:Label ID="lblMainImgId" runat="server" Text='<%# Eval("ImageId") %>' ></asp:Label>
<asp:Label ID="lblMainImgName" runat="server" Text='<%# Eval("ImageName") %>' ></asp:Label> <br />
<asp:Image ID="imgMain" runat="server" Height="250" Width="250" ImageUrl='<%# Eval("ImagePath") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label runat="server" Font-Bold="true" Text="Current main image" ></asp:Label> <br />
<asp:Image ID="imgMain" runat="server" Height="250" Width="250" ImageUrl='<%# Eval("ImagePath") %>' /> <br />
<asp:Label runat="server" Font-Bold="true" Text="Upload a new image to replace the current main image." ></asp:Label> <br />
<asp:FileUpload ID="flupEditMain" runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:FileUpload ID="flupMain" runat="server" AllowMultiple="false" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Supporting Images" FooterText="Supporting Images" HeaderText="Supporting Images">
<ItemTemplate>
<asp:Label ID="lblSupImages" runat="server" Visible="false" Font-Bold="true" Text="Select images to delete"></asp:Label><br />
<asp:GridView
ID="grdSupImages"
runat="server"
ShowHeader="false"
CellPadding="4"
ForeColor="#333333"
GridLines="None"
AutoGenerateColumns="False"
AllowPaging="true"
PageSize="4"
OnPageIndexChanging="grdSupImages_PageIndexChanging"
OnRowEditing="grdSupImages_RowEditing"
EnableViewState="true"
DataKeyNames="productID"
RowStyle-VerticalAlign="Top"
RowStyle-HorizontalAlign="Center"
EmptyDataText="No Supporting Images Found">
<AlternatingRowStyle BackColor="White" ForeColor="#284775"></AlternatingRowStyle>
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:Image ID="imgSup" runat="server" ToolTip='<%# Eval("ImgId") %>' AlternateText='<%# Eval("ImageName") %>' ImageUrl='<%# Eval("ImagePath") %>' Height="125" Width="125" />
<asp:Label ID="imgSupName" runat="server" Text='<%# Eval("ImageName") %>' AssociatedControlID="imgSup"></asp:Label>
<asp:CheckBox ID="chkSupImages" runat="server" Visible="false" Text="Select Image" CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999"></EditRowStyle>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></FooterStyle>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></HeaderStyle>
<PagerStyle HorizontalAlign="Center" BackColor="#284775" ForeColor="White"></PagerStyle>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333"></RowStyle>
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"></SelectedRowStyle>
<SortedAscendingCellStyle BackColor="#E9E7E2"></SortedAscendingCellStyle>
<SortedAscendingHeaderStyle BackColor="#506C8C"></SortedAscendingHeaderStyle>
<SortedDescendingCellStyle BackColor="#FFFDF8"></SortedDescendingCellStyle>
<SortedDescendingHeaderStyle BackColor="#6F8DAE"></SortedDescendingHeaderStyle>
</asp:GridView>
<div style="clear:both;"> </div>
<asp:Label ID="lblFlupSupImages" runat="server" Font-Bold="true" Text="Add extra images" Visible="false" />
<br />
<asp:FileUpload ID="flupSupImages" runat="server" AllowMultiple="true" Visible="false" />
</ItemTemplate>
<FooterTemplate>
<asp:FileUpload ID="flupExtra" runat="server" AllowMultiple="true" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
<br />
<span onclick="return confirm('Are you sure you want to delete these images?')">
<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
</span>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
<br />
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddRecord" runat="server" Text="Add" CommandName="Add"></asp:Button>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999"></EditRowStyle>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></FooterStyle>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></HeaderStyle>
<PagerStyle HorizontalAlign="Center" BackColor="#284775" ForeColor="White"></PagerStyle>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333"></RowStyle>
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"></SelectedRowStyle>
<SortedAscendingCellStyle BackColor="#E9E7E2"></SortedAscendingCellStyle>
<SortedAscendingHeaderStyle BackColor="#506C8C"></SortedAscendingHeaderStyle>
<SortedDescendingCellStyle BackColor="#FFFDF8"></SortedDescendingCellStyle>
<SortedDescendingHeaderStyle BackColor="#6F8DAE"></SortedDescendingHeaderStyle>
</asp:GridView>
My code behind:
protected void grdImages_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView grd = (GridView)e.Row.FindControl("grdSupImages"); // find controls
Label prodId = (Label)e.Row.FindControl("lblProdId");
grd.ToolTip = prodId.Text;
int product = Convert.ToInt32(prodId.Text); // assign values to variables.
BindNestedGrid(product, grd); // call the function.
}
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex == grdImages.EditIndex)
{
//Find Child GridView
GridView supImagesGrd = (GridView)e.Row.Cells[2].FindControl("grdSupImages");
if (supImagesGrd != null)
{
// find grid header label and make visible
Label gridHead = (Label)e.Row.Cells[2].FindControl("lblSupImages");
gridHead.Visible = true;
// find fileupload header label and make visible.
Label fileUpHead = (Label)e.Row.Cells[2].FindControl("lblFlupSupImages");
fileUpHead.Visible = true;
// find fileupload control and make it visible.
FileUpload flup = (FileUpload)e.Row.Cells[2].FindControl("flupSupImages");
flup.Visible = true;
//Loop through the GridView
foreach (GridViewRow row in supImagesGrd.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
//Find the CheckBox
CheckBox chb = (CheckBox)row.Cells[0].FindControl("chkSupImages");
if (chb != null)
{
chb.Visible = true;
}
}
}
}
}
}
protected void grdSupImages_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gv = (GridView)sender;
gv.PageIndex = e.NewPageIndex;
BindNestedGrid(Convert.ToInt32(gv.ToolTip), gv);
}
If any more information or code is required please let me know.

For anyone who wants to know, I disabled paging of the child grid when edit button is clicked so all check boxes show on all rows of child grid now.

Related

Pass Data from inner grid View to main grid view (gridview in gridview template)ASP.net C#

Am face some troubles to pass data from inner grid to main grid as shown in this pic
im tried too many solution i can found using C# and Jquery
this is simple photo to result
Now this is invoice read from database to show multiple data as shown the grid contain a another grid by normal code it was impossible to access to gridview2 to get data
all i want is get "amount" from gridview2 then calculate "sum" then pass it to "total" in gridview1
also im trid with Jquery it do nothing this is simple code of both fields i want to do sum with Jquery
<script type="text/javascript">
$(function () {
var fields = document.getElementsByClassName('clstocal');
var sum = 0;
for (var i = 0; i < fields.length; ++i) {
var item = fields[i];
sum += parseInt(item.innerHTML);
}
$("#totalprice").text(sum);
}); // this code get it from this site
</script>
<asp:TextBox ID="amount" CssClass ="clstocal" autopostback="true" runat="server" Enabled="False" Height="24px" Text='<%# Bind("body_total") %>' Width="98px" OnTextChanged="amount_TextChanged"></asp:TextBox>
and
<asp:TextBox ID="totalprice" runat="server" Enabled="False" Height="22px" Width="145px" OnTextChanged="totalprice_TextChanged" autopostback="true"></asp:TextBox>
there are a easy solution but not a good solution is sum the result to database then read it again but this is not efficient at all ..
To be honest the answer was simple
1. its impossible to access grid inside grid
2. solved by normal code in C# as shown
protected void GridHead(object sender, GridViewRowEventArgs e)
{
// MAin Grid which is grid number 1
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox textboxPrice1 = e.Row.FindControl("totalprice") as TextBox;
textboxPrice1.Text = i.ToString();
i = 0; // Global double -> protected double i = 0; // THIS LINE TO STOP COUNTING TO OTHER NEXT INVOICE
}
}
protected void GridBody(object sender, GridViewRowEventArgs e)
{
// Inner grid which contain multipe item need to sun then send it to Main grid
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox lblPrice2 = e.Row.FindControl("amount") as TextBox;
i += Convert.ToDouble(lblPrice2.Text);
}
}
and the code behind to handle the grid
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Reports._Default" %>
Welcome to Invoice Revision
<!-- change both grid from OnSelectedIndexChanged to onrowdatabound to use GridViewRowEventArgs instade EventArgs-->
<asp:GridView ID="GD_Head" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Height="206px" Width="548px" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None" onrowdatabound="GridHead">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<br />
<asp:Label ID="Label25" runat="server" Height="22px" Text="Invoice : " Width="60px"></asp:Label>
<asp:Label ID="Label1" width="145px" runat="server" Text='<%# Eval("invoice") %>' BorderStyle="Solid" BorderWidth="1px" Height="22px" Font-Size="Smaller"></asp:Label>
<asp:Label ID="Label26" runat="server" Height="22px" Text="Kind : " Width="60px"></asp:Label>
<asp:Label ID="Label35" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("kind") %>' Width="71px"></asp:Label>
<asp:Label ID="Label27" runat="server" Height="22px" Text="Date : " Width="60px"></asp:Label>
<asp:Label ID="Label3" width="170px" runat="server" Text='<%# Eval("date") %>' BorderWidth="1px" Height="22px"></asp:Label>
<br />
<br />
<asp:Label ID="Label28" runat="server" Height="22px" Text="File : " Width="60px"></asp:Label>
<asp:Label ID="Label4" width="144px" runat="server" Text='<%# Eval("file") %>' BorderWidth="1px" Height="22px"></asp:Label>
<asp:Label ID="Label29" runat="server" Height="22px" Text="Awb" Width="60px"></asp:Label>
<asp:Label ID="Label5" width="313px" runat="server" Text='<%# Eval("awb") %>' BorderWidth="1px" Height="22px"></asp:Label>
<br />
<br />
<asp:Label ID="Label30" runat="server" Height="22px" Text="GL : " Width="60px"></asp:Label>
<asp:Label ID="Label6" width="144px" runat="server" Text='<%# Eval("gl") %>' BorderWidth="1px" Height="22px"></asp:Label>
<asp:Label ID="Label7" width="376px" runat="server" Text='<%# Eval("name") %>' BorderWidth="1px" Height="23px"></asp:Label>
<br />
<br />
<asp:Label ID="Label31" runat="server" Height="22px" Text="Total : " Width="60px"></asp:Label>
<!-- Convert String to int then sum them from body to head -->
<asp:TextBox ID="totalprice" OnTextChanged ="totalprice_TextChanged" runat="server" Enabled="False" Height="22px" Width="145px"></asp:TextBox>
<asp:Label ID="lb12" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("curId") %>' Width="38px"></asp:Label>
<asp:Label ID="Label32" runat="server" Height="22px" Text="Rate : " Width="40px"></asp:Label>
<asp:Label ID="Label9" width="100px" runat="server" Text='<%# Eval("rate") %>' BorderStyle="Solid" BorderWidth="1px" Height="22px"></asp:Label>
<asp:Label ID="Label33" runat="server" Height="22px" Text="Emp" Width="40px"></asp:Label>
<asp:Label ID="lb10" width="65px" runat="server" Text='<%# Eval("emp") %>' BorderStyle="Solid" BorderWidth="1px" Height="22px"></asp:Label>
<asp:Label ID="Label34" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("poste") %>' Width="62px"></asp:Label>
<br />
<br />
<asp:Label ID="Label14" runat="server" Height="22px" Text="No" Width="52px"></asp:Label>
<asp:Label ID="Label23" width="76px" runat="server" Text='GL' Height="22px"></asp:Label>
<asp:Label ID="Label18" runat="server" Height="22px" Text="Description" Width="250px"></asp:Label>
<asp:Label ID="Label19" runat="server" Height="22px" Text="Amount" Width="130px"></asp:Label>
<!-- change both grid from OnSelectedIndexChanged to onrowdatabound to use GridViewRowEventArgs instade EventArgs-->
<asp:GridView ID="GD_Body" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="serial" DataSourceID="SqlDataSource2" Height="39px" onrowdatabound="GridBody" Width="629px" GridLines="None">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Label ID="lb40" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("line_no") %>' Width="52px"></asp:Label>
<asp:Label ID="Label41" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("body_gl") %>' Width="76px"></asp:Label>
<asp:Label ID="Label42" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("body_name") %>' Width="250px"></asp:Label>
<asp:TextBox ID="amount" CssClass ="clstocal" runat="server" Enabled="False" Height="24px" Text='<%# Bind("body_total") %>' Width="98px"></asp:TextBox>
<asp:Label ID="UN" runat="server" BorderWidth="1px" Height="22px" Text="UN" Width="52px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Label ID="Label22" runat="server" Text="---------------------------------------------------------------------------------------------------------------------" Width="600px"></asp:Label>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Body %>" SelectCommand="SELECT * FROM [body] WHERE ([invoice] = #invoice)">
<SelectParameters>
<asp:ControlParameter ControlID="Label1" Name="invoice" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<br />
</ItemTemplate>
<ControlStyle Font-Bold="True" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Head %>" SelectCommand="SELECT * FROM [head]"></asp:SqlDataSource>
and this is result
I hope this answer find you
if you have better answer please share with us

Changing dropdownlist selected index in Gridview, conected (to getting value from) second DropDownlist

My page which to do this:
I want to connect outside dropdown list to inside of gridview dropdownlist. When selec item from outside dropdownlist, must be change inside dropdown list item automaticaly. I've tried a lot thing to change. Can you give me help. Thanks.
My ASPX page.(Don't answer connected this code)
<asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="Kimlik" GridLines="Horizontal"
onrowdatabound="GridView4_RowDataBound" ForeColor="#333333" Visible="False">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Öd.Nu.">
<EditItemTemplate>
<asp:TextBox ID="TextBox1x" runat="server" Text='<%# Bind("Kimlik") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1x" runat="server" Text='<%# Bind("Kimlik") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Öğrenci Adı Soyadı">
<EditItemTemplate>
<asp:TextBox ID="TextBox2x" runat="server" Text='<%# Bind("OgrenciAdiSoyadi") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2x" runat="server" Text='<%# Bind("OgrenciAdiSoyadi") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Odev Sonucu">
<EditItemTemplate>
<asp:TextBox ID="TextBox3x" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1x" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ödev Sonuç Açıklaması">
<EditItemTemplate>
<asp:TextBox ID="TextBox4x" runat="server" Text='<%# Bind("SonucAciklama") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4x" runat="server" Text='<%# Bind("SonucAciklama") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
on Selected Index Change Event of Outer drop down add below code
protected void OurterDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView.Rows)
{
DropDownList innerDropdown = (DropDownList)row.FindControl("innedDropedDOwnID");
innerDropdown .ClearSelection(); //making sure the previous selection has been cleared
innerDropdown .Items.FindByValue(OurterDropDown.SelectedValue).Selected = true;
}
}
Make sure you have set AutoPostBack="true" outside DropDownList and then try this code in RowDataBound event:
protected void GridView4_RowDataBound(object sender, GridViewRowEventArgs e)
{
// check if your row is not a Header/Footer row
if (e.Row.RowType == DataControlRowType.DataRow)
{
// get Dropdownlist from gridview
DropDownList DropDownList1x = e.Row.FindControl("DropDownList1x") as DropDownList;
// ddlouter is your outside DropDownList
DropDownList1x.Items.FindByValue(ddlOuter.SelectedValue).Selected = true;
}
}

go to specific page on button click in asp gridview

I am trying to implement GO TO page.Means when i enter any page number in text box "txtGoToPage" and clicking button "btnGo" it should take me to that page.
<asp:Label ID="lblGoToPage" runat="server" Text="Go To Page : "></asp:Label>
<asp:TextBox ID="txtGoToPage" runat="server" Width="47px"></asp:TextBox>
<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGo_Click" />
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" OnRowDataBound="gv_RowDataBound" OnRowDeleting="gv_RowDeleting" OnRowEditing="gv_RowEditing" CellPadding="4" ForeColor="#333333" OnRowCreated="gv_RowCreated" OnRowCommand="gv_RowCommand" OnRowUpdating="gv_RowUpdating" AllowPaging="true" OnPageIndexChanging="gv_PageIndexChanging" GridLines="Both" CssClass="GridViewStyle" class="ui-widget-content" Style="width: 100%; padding: 2px" OnSelectedIndexChanged="gv_SelectedIndexChanged">
<EditRowStyle CssClass="GridViewEditRow" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<PagerStyle CssClass="pager" />
<Columns>
<asp:TemplateField Visible="false" HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%#Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Correct">
<ItemTemplate>
<asp:Label ID="lblCorrect" runat="server" Text='<%#Eval("correct") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCorrect" runat="server" Width="40px" Text='<%#Eval("correct") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comments">
<ItemTemplate>
<asp:Label ID="lblComments" runat="server" Text='<%#Eval("comments") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtComments" runat="server" TextMode="multiline" Width="50px" Columns="50" Rows="5" Text='<%#Eval("comments") %>' />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings FirstPageText="First" LastPageText="Last"
Mode="Numeric" PageButtonCount="20" PreviousPageText="Previous" />
</asp:GridView>
Thanks in advance.
Sample answer:
protected void btnGo_Click(object sender, EventArgs e)
{
GridView1.PageIndex = Convert.ToInt16(txtGoToPage.Text) -1; //since PageIndex starts from 0 by default.
txtGoToPage.Text = "";
GridView1.DataBind()
}

why on clicking on edit button of childgridview the childgridview hides?

This is what I have done for nested gridview :
The jQuery I have used to maintain this show hide is as:
<script type="text/javascript">
// Method for managing opening of gridview on + image and - image
$("[src*=plus]").live("click", function() {
$(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
$(this).attr("src", "../Image/minus.gif");
});
$("[src*=minus]").live("click", function() {
$(this).attr("src", "../Image/plus.gif");
$(this).closest("tr").next().remove();
});
</script>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" DataKeyNames="num"
AutoGenerateColumns="False" Style="font-size: x-small" OnSelectedIndexChanging="GridView1_SelectedIndexChanging"
OnRowCancelingEdit="GridView1_RowCancelling" OnRowUpdating="GridView1_RowUpdating" OnRowCommand="GridView1_RowCommand"
OnRowDeleting="GridView1_RowDeleting" ShowFooter="True"
OnRowEditing="GridView1_RowEditing" OnRowDataBound="GridView1_RowDataBound" OnSorting="GridView1_Sorting"
AllowSorting="true">
<RowStyle BackColor="#E3EAEB" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt="" style="cursor: pointer" src="../Image/plus.gif" />
<asp:Panel ID="pnlOrders" style="display:none;">
<asp:GridView ID="GridView3" runat="server" CellPadding="4" ForeColor="#333333" DataKeyNames="sno"
OnRowCancelingEdit="GridView3_RowCancelling" OnRowUpdating="GridView3_RowUpdating"
OnRowEditing="GridView3_RowEditing" AutoGenerateColumns="False" Style="font-size: x-small">
<RowStyle BackColor="#E3EAEB" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField>
<HeaderTemplate>
Detailed Head
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="empdetails" Width="200px" runat="server" Text='<%# Eval("Details") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtempdetail" Width="200px" runat="server" MaxLength="9" Text='<%# Eval("Details") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="sal" HeaderText="salary" />
<asp:CommandField ShowEditButton="true" EditText="Edit" />
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="View" runat="server" Text="View" CommandName="view"
CommandArgument='<%#Container.DataItemIndex+1 %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Age</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblage" Width="80px" runat="server" MaxLength="4" Text='<%# Eval("age") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtage" runat="server" Text='<%# Eval("age") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
BloodGroup</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblBloodGroup" Width="100px" runat="server" MaxLength="9" Text='<%# Eval(" BloodGroup") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBloodGroup" runat="server" Text='<%# Eval(" BloodGroup") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" SelectText="Select" />
<asp:CommandField DeleteText="Reject" ShowDeleteButton="True" />
<asp:CommandField ShowEditButton="true" EditText="Edit" />
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
On server side this is what I am doing:
protected void GridView3_RowEditing(object sender, GridViewEditEventArgs e)
{
DataSet ds = new DataSet();
tBL objBL = new tBL();
GridViewRow editedRow = GridView1.Rows[e.NewEditIndex];
//now search row for your control...
GridView GridView3 = (GridView)editedRow.FindControl("GridView3");
GridView3.EditIndex = e.NewEditIndex;
objtxtToTableBL.ViewBL(dt);
GridView3.DataSource = dt;
GridView3.DataBind();
}
protected void GridView3_RowCancelling(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
}
protected void GridView3_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
I am binding the nested gridview on rowdatabound of GridView1 and that works fine.
The problem with all this is when I click on the edit button of innergridview(child) it get hide as the page postback .
What should I do so that when I click on its edit button the child gridview remains open and it gets hides only when I click on minus button. Please help it has already taken 3 days of mine.

Import csv file into existing gridview

I have a tabcontainer with two tabs:
The first tab contains a button.
The second tab contains a gridview that has three textboxes.
I would like to do the following: when one clicks on the button in tab1, the gridview in tab2 is populated with data in a prespecified csv file (note that the file has the same column names as the gridview).
This is what I have so far, but for some reason, the gridview doesn't get populated with the data in the csv.
aspx.cs
<asp:UpdatePanel ID="WholeUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" HeaderText=""
Width="900px" TabStripPlacement="Top" ScrollBars="None" UseVerticalStripPlacement="false"
VerticalStripWidth="120px" BackColor="White" BorderColor="White"
Style="margin-right: 84px">
<asp:TabPanel ID="OptionPanel" runat="server" Height="600px">
<HeaderTemplate>
Simulation Option
</HeaderTemplate>
<ContentTemplate>
<asp:Button ID="PreviousSimButton" runat="server" Text="Run Previous Simulation" Width="250px" OnClick="PreviousSimButton_OnClick" />
<ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="TabPanel1" runat="server" Height="600px" >
<HeaderTemplate>
General
</HeaderTemplate>
<ContentTemplate>
<asp:UpdatePanel ID="TestUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="GeneralPanel" runat="server" Height="274px">
<div style="overflow: auto; height: 222px; width: 100%">
<asp:GridView ID="InflationGridView" runat="server" AutoGenerateColumns="False" Width="52%"
ShowHeaderWhenEmpty="True" CellPadding="4" ForeColor="#333333" GridLines="None"
AllowSorting="True" ShowFooter="True">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" Height="2px" />
<Columns>
<asp:TemplateField HeaderText="Start Year">
<ItemStyle Font-Size="13px" Width="20%" HorizontalAlign="Center" />
<ItemTemplate>
<asp:TextBox ID="StartInflationTextBox" runat="server" Width="60px" Text="" Style="text-align: center;"></asp:TextBox>
<asp:NumericUpDownExtender ID="StartInflationNumericUpDownExtender" runat="server"
TargetControlID="StartInflationTextBox" Minimum="1" Width="60">
</asp:NumericUpDownExtender>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="End Year">
<ItemStyle Font-Size="13px" Width="20%" HorizontalAlign="Center" />
<ItemTemplate>
<asp:TextBox ID="EndInflationTextBox" runat="server" Width="60px" Text="" Style="text-align: center;"></asp:TextBox>
<asp:NumericUpDownExtender ID="EndInflationNumericUpDownExtender" runat="server"
TargetControlID="EndInflationTextBox" Minimum="1" Width="60">
</asp:NumericUpDownExtender>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Inflation Rate">
<ItemStyle Font-Size="13px" Width="25%" HorizontalAlign="Center" Height="2px" />
<ItemTemplate>
<asp:TextBox ID="InflationTextBox" runat="server" Text="" Width="60px" Style="text-align: center;"></asp:TextBox>
%
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="AddNewInflationRowButton" runat="server" Text="Add New Row" OnClick="AddNewInflationRowButton_Click"
Height="25px" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle Font-Bold="True" ForeColor="White" Height="20px" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" Height="10px" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</contenttemplate>
</asp:TabPanel>
</asp:TabContainer>
</ContentTemplate>
</asp:UpdatePanel>
aspx.cs
protected void PreviousSimButton_OnClick(object sender, EventArgs e)
{
TabContainer1.ActiveTabIndex = 1;
string file = "C:\\inst_research\\MonteCarlo\\Data\\inflation.csv";
InflationGridView.DataSource = (DataTable)ReadToEnd(file);
InflationGridView.DataBind();
WholeUpdatePanel.Update();
TestUpdatePanel.Update();
}
private object ReadToEnd(string filePath)
{
DataTable dtDataSource = new DataTable();
string[] fileContent = File.ReadAllLines(filePath);
if (fileContent.Count() > 0)
{
string[] columns = fileContent[0].Split(',');
for (int i = 0; i < columns.Count(); i++)
{
dtDataSource.Columns.Add(columns[i]);
}
for (int i = 1; i < fileContent.Count(); i++)
{
string[] rowData = fileContent[i].Split(',');
dtDataSource.Rows.Add(rowData);
}
}
return dtDataSource;
}
Your code should not give any issues if you use :
fileContent.Length instead of fileContent.Count()

Categories

Resources