So I have created a gridview and inside it I have given another gridview.
<asp:GridView ID="dgInstitute" runat="server" AutoGenerateColumns="False" CellPadding="0"
CellSpacing="0" ShowHeaderWhenEmpty="True" PageSize="100" Width="100%" CssClass="table table-responsive table-bordered"
Visible="true" UseAccessibleHeader="true" OnRowCreated="dgInstitute_RowCreated"
OnDataBound="dgInstitute_DataBound" OnRowDataBound="dgInstitute_RowDataBound">
<Columns>
<asp:BoundField HeaderText="Id" DataField="refGroupId" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="gridHeader panel-default"></asp:BoundField>
<asp:TemplateField HeaderStyle-CssClass="gridHeader panel-default">
<ItemTemplate>
<asp:GridView ID="dgProgram" runat="server" AutoGenerateColumns="False" CellPadding="0"
CellSpacing="0" ShowHeaderWhenEmpty="True" PageSize="100" Width="100%" CssClass="table table-responsive table-bordered"
Visible="true" UseAccessibleHeader="true" OnRowCreated="dgProgram_RowCreated" OnDataBound="dgProgram_DataBound">
<Columns>
<asp:BoundField HeaderText="Id" DataField="refGroupId" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="gridHeader panel-default"></asp:BoundField>
<asp:TemplateField HeaderStyle-CssClass="gridHeader panel-default" ShowHeader="false">
<ItemTemplate>
<asp:Button AutoPostBack="true" Width="300px" CssClass="btn btn-primary btn-embossed" ID="btnProgram" runat="server"
Text='<%#Eval("refValues") %>'
OnClick="btnProgram_Click" CommandArgument='<%#Eval("refGroupId") %>' />
<div class="voffset3"></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="panel-default"></HeaderStyle>
<RowStyle CssClass=""></RowStyle>
</asp:GridView>
<div class="voffset3"></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="panel-default"></HeaderStyle>
<RowStyle CssClass=""></RowStyle>
</asp:GridView>
For this first gridview is accessible on code behind but when I try to use secondGridview it says dgProgram name doe not exist in current context.
I restarted my solution but didn't work.
You need to use FindControl on the parent GridView by a row index.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
//bind the datasource for the parent gridview
dgInstitute.DataSource = mySource1;
dgInstitute.DataBind();
//find the nested gridview in row 4 and cast it
GridView nestedGridView = dgInstitute.Rows[3].FindControl("dgProgram") as GridView;
//bind the datasource for the nested gridview
nestedGridView.DataSource = mySource2;
nestedGridView.DataBind();
}
}
Or in the RowDataBound event of the parent GridView.
protected void dgInstitute_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//find the nested gridview in row 4 and cast it
GridView nestedGridView = e.Row.FindControl("dgProgram") as GridView;
//bind the datasource for the nested gridview
nestedGridView.DataSource = mySource2;
nestedGridView.DataBind();
}
}
Related
Dear all genius please help me with this had been trying everywhere but no luck. I am trying to open my nested gridview for a single Parent Row but gets open the entire parent row with one single record of the child row. It should be when I click the button of the Parent gridview row then the record for the particular row should display in the Child gridview in the same Parent row, but in my case the record of the Parent row shows up and the Child gridview gets open in all the parent row. Please need in help :(
enter image description here
<asp:GridView runat="server" ID="GridView4" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="UId" class="" CellPadding="4" PageSize="5">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="File Type" Visible="false">
<ItemTemplate>
<asp:Label ID="lblFileType" runat="server" Text='<%# Eval("FileType") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Image Name" Visible="false">
<ItemTemplate>
<asp:Label ID="lblImageName" runat="server" Text='<%# Eval("ImageName") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:ImageButton ID="Image2" class="img1" runat="server" ImageUrl='<%# Bind("pic") %>' OnCommand="blah_Command" CommandName='<%# Eval("UId") %>' CommandArgument='<%# Eval("pic") %>' />
<asp:Panel ID="pnlOrders" runat="server" Style="position: relative" Visible="false">
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="FileSize" HeaderText="Order Id" />
<asp:BoundField ItemStyle-Width="150px" DataField="ShareByUserId" HeaderText="Date" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#ffffff" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#ffffff" Font-Bold="True" ForeColor="White" />
</asp:GridView>
Code Behind:
protected void blah_Command(object sender, CommandEventArgs e)
{
foreach (GridViewRow row2 in GridView4.Rows)
{
row2.FindControl("pnlOrders").Visible = true;
string customerId = e.CommandName.ToString();
GridView gvOrders = row2.FindControl("gvOrders") as GridView;
BindOrders(customerId, gvOrders);
}
}
private void BindOrders(string customerId, GridView gvOrders)
{
gvOrders.ToolTip = customerId;
gvOrders.DataSource = GetData(string.Format("select * from SShare where UId='{0}'", customerId));
gvOrders.DataBind();
}
You are looping all the rows in GridView4, so every nested GridView will be filled with data.
What you need is the row number of the row that the clicked LinkButton is in, and use that to find the nested GridView.
protected void blah_Command(object sender, CommandEventArgs e)
{
//get the current datagrid item from the sender
GridViewRow row = (GridViewRow)(((Control)sender).NamingContainer);
//the row index of the gridviewrow
int rowIndex = row.RowIndex;
//find the correct nested grid using the row index
GridView gvOrders = GridView4.Rows[rowIndex].FindControl("gvOrders") as GridView;
}
I m making a website in which i have used grid view table and retrieved data from database table category as shown down :
Database table category
I want to create link on rows i.e family tours ,religious tour ,Adventure Tours,Special Event Tours,National Park and whenever new row is created in table hyperlink should be created automatically on it .... How should I do it Please do help .... thank you
I want create link on data that is inside the red mark
Try this, I develop for you:
.aspx
<asp:GridView ID="GridView1" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Page
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("page") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Navigation
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="hdn_navigation" Value='<%# Bind("navigation") %>' runat="server" />
<asp:LinkButton ID="LinkButton1" CommandName="navigation" runat="server">Click Me</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind .cs:
private void load_gridView()
{
DataTable dt = new DataTable();
dt.Columns.Add("page");
dt.Columns.Add("navigation");
DataRow dr = dt.NewRow();
dr["page"] = "family tours";
dr["navigation"] = "family_tours.aspx";
dt.Rows.Add(dr);
DataRow dr2 = dt.NewRow();
dr2["page"] = "religious tour";
dr2["navigation"] = "religious_tour.aspx";
dt.Rows.Add(dr2);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
HiddenField hdn_navigation = (HiddenField)row.FindControl("hdn_navigation");
if (e.CommandName.ToString() == "navigation")
{
Response.Redirect(hdn_navigation.Value);
}
}
calling function from Page_Load():
protected void Page_Load(object sender, EventArgs e)
{
load_gridView();
}
Thank you for your help really appreciate your help #Rana Ali
I have got a more simplified method that can be done by just writing this code I hope this will help others as well :)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" GridLines="None" Height="300px"
Width="100px">
<Columns>
<asp:BoundField DataField="Cat_name" HeaderText="Category"
SortExpression="Cat_name" >
<HeaderStyle Font-Bold="True" Font-Italic="True"
Font-Names="Lucida Calligraphy" Font-Size="X-Large" ForeColor="#3399FF" />
<ItemStyle Font-Bold="True" Font-Italic="True" Font-Size="Small" />
</asp:BoundField>
<asp:HyperLinkField DataTextFormatString="View" DataTextField="Cat_url" DataNavigateUrlFields="Cat_url" HeaderText="" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ToursandTravelsConnectionString %>"
SelectCommand="SELECT [Cat_name], [Cat_url] FROM [category]">
This is and output of the code above :
I have a GridView that has data bounded rows. I'm trying to get specific cell value on the SelectedIndexChanged event of DropDownList. My tries are as follows:
string temp= GridView2.SelectedRow.Cells[3].Text;
string temp = ((DataBoundLiteralControl)GridView2.Rows[0].Cells[3].Controls[0]).Text;
DataBoundLiteralControl dblc = (DataBoundLiteralControl)GridView2.Rows[0].Cells[3].Controls[0];
string temp=dblc.Text;
These all 3 of them returns null.
Moreover, the Control[0] is returning correct value of TemplateFields only, but not DataBound fields.
.aspx
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor=" #d54d7b" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#FFFAFC" AlternatingRowStyle-BackColor="#FFFFF7" AlternatingRowStyle-ForeColor="#000"
AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging" Width="900px" Font-Names="Segoe UI Light" BorderColor="#DEDEDE">
<RowStyle HorizontalAlign="center" />
<Columns>
<asp:BoundField DataField="Description" HeaderText="Description" ItemStyle-Width="80" />
<asp:BoundField DataField="Vacancies" HeaderText="Vacancies" ItemStyle-Width="150" />
<asp:TemplateField HeaderText="Detail">
<ItemTemplate>
<asp:BoundField DataField="Date" HeaderText="Date" ItemStyle-Width="80" />
<asp:BoundField DataField="Time" HeaderText="Time" ItemStyle-Width="80" />
</Columns>
Problem you are probably facing is that, GridView1 hasn't been bounded yet.. that is the reason why Control[0] is returning correct value of TemplateFields but cells in the gridview returns null.
You can do the following
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
/// send value to filter or bind gridView1
/// bind gridvew
string temp = GridView1.Rows[0].Cells[3].Text;
/// or any code to get values from GridView Cell
}
I Converted all <asp:BoundFields> to <asp:TemplateField> and it looked like this:
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor=" #009C0D" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#FFFAFC" AlternatingRowStyle-BackColor="#FFFFF7" AlternatingRowStyle-ForeColor="#000"
AutoGenerateColumns="false" AllowPaging="true" CssClass="test"
HtmlEncode="true"
Font-Names="Segoe UI Light" BorderColor="#DEDEDE" >
<RowStyle HorizontalAlign="center" />
<Columns>
<asp:TemplateField HeaderText="Description" ControlStyle-Width="250px" ><ItemTemplate> <asp:LinkButton ID= "Description" PostBackUrl='<%# Eval("Description", "~/{0}.aspx") %>' Text='<%# Eval("Description")%>' runat="server" ></asp:LinkButton> </ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Vacancies"><ItemTemplate> <asp:Label ID="Vacancies" Text='<%# Eval("Vacancies")%>' runat="server" ></asp:Label> </ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Detail"><ItemTemplate><asp:Label ID="City" Text='<%# Eval("City")%>' runat="server" ></asp:Label> <div style="font-size:10px"> <asp:Label ID="Label1" Text='<%# Eval("DateDay")%>' runat="server" ></asp:Label> </div> </ItemTemplate></asp:TemplateField>
//and others
</Columns>
</asp:GridView>
And get specific value by entering this code behind DropDownList:
if (dropdown.SelectedIndex != -1)
{
ListItem mySelectedItem = (from ListItem li in dropdown.Items where li.Selected == true select li).First();
foreach (GridViewRow rw in GridView2.Rows)
{
Label tv = (Label)rw.Cells[3].FindControl("City");
if (tv.Text.IndexOf(mySelectedItem.Text) != -1)
{
rw.Visible = true;
}
else
rw.Visible = false;
}
}
I have two GridViews. GridView1 contains 2 link buttons in a column. When I select a link button in GridView1, the details of the link button should be displayed in GridView2.
GridView2 contains a column with radio buttons. I need to fill the radio buttons dynamically from the database.
How can I fill the Radio button list of GridView2 by using GridView1_RowCommand? Or Can I get it from the RowDataBound event of GridView2?
code behind:
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Yes")
{
GridViewRow gvRow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int RowIndex = gvRow.RowIndex;
Int32 iAppID = Convert.ToInt32(GridView1.DataKeys[gvRow.RowIndex].Value.ToString());
dset = userApps.UserSelectedApp(iUserID, iAppID);
if (dset.Tables[0].Rows.Count > 0)
{
GridViewRow gRow = GridView2.Rows[RowIndex];//I need to create object to this Gridview2, and fill the radiobutton list with some values
RadioButtonList rdbtnSubPlans = (RadioButtonList)e.gRow.Cells[2].FindControl("rdbSubPlans");
ds = userApps.UpgradePlans(iUserID, iAppID);
if (ds != null)
{
rdbtnSubPlans.DataSource = ds;
rdbtnSubPlans.DataValueField = "PlanID";
rdbtnSubPlans.DataTextField = "Plans";
rdbtnSubPlans.DataBind();
}
}
}
if (e.CommandName == "No")
{}
dtset = user.UserSelectedAppication(iUserID, iAppID);
GridView2.DataSource = dtset;
GridView2.DataBind();
MultiView1.SetActiveView(viewRenewOrUpdate);
}
}
ASPX code for the GridViews
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ApplicationID"
OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand"
OnSorting="GridView1_Sorting" OnDataBound="GridView1_DataBound"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="S.No" ItemStyle-HorizontalAlign="center" HeaderStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:Label ID="l1" runat="server" Text="1"></asp:Label>
</ItemTemplate>
<HeaderStyle Width="10%" />
</asp:TemplateField>
< Some Bound Fields>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="lnkYes" runat="server" Text="Yes"
CssClass="lnkbtn" Visible="false" commandname="Yes" Width="100px" >
</asp:LinkButton>
<asp:LinkButton ID="lnkNo" runat="server" CssClass="lnkbtn" Text="No"
Visible="false" commandname="No" ToolTip="No and Yes current plan" Width="100px" >
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="GridView2" runat="server" DataKeyNames="ApplicationID"
OnRowDataBound="GridView2_RowDataBound">
<Columns>
<asp:BoundField DataField="ApplicationName" HeaderText="Application name">
<HeaderStyle Width="30%" />
<ItemStyle CssClass="col" />
</asp:BoundField>
<asp:TemplateField HeaderText="Plans">
<ItemTemplate>
<asp:RadioButtonList ID="rdbSubPlans" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="rdbSubPlan_OnSelectedIndexChanged" Enabled="false">
</asp:RadioButtonList>
</ItemTemplate>
<ItemStyle CssClass="col" />
</Columns>
</asp:GridView>
Fill Gridview2 first, and then do the following corrections:
GridViewRow gRow = GridView2.Rows[0]
RadioButtonList rdbtnPlans = (RadioButtonList)gRow.FindControl("rdbPlans");
I have an issue where the pager number doesn't get updated automatically when I hide rows in the gridview. Do I need to set the pager value manually? Can someone suggest me on this?
ASPX page
<asp:GridView ID="SearchResults" runat="Server" AutoGenerateColumns="false"
EnableViewState="false" AllowPaging="true" PageSize="50"
OnDataBound ="SearchResults_DataBound"
OnRowDataBound="SearchResults_RowDataBound">
<RowStyle CssClass="EvenRow" />
<AlternatingRowStyle CssClass="OddRow" />
<Columns>
<asp:TemplateField meta:resourceKey="UmSellField">
<ItemStyle CssClass="alpha" />
<HeaderStyle CssClass="alpha" />
<ItemTemplate>
<asp:Label ID="UmSellLabel" runat="server" EnableViewState="false"
Text='<%# GetUnitOfMeasure(Container.DataItem,false) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
codebehind
protected void SearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType.Equals(DataControlRowType.DataRow))
{
e.Row.Visible = showRow;
e.Row.Cells[0].Visible = showRow;
}
}
ShowRow is a boolean value that gets set in GetUnitOfMeasure function (not copied here) based on these conditions.