Hello, i watch and saw many examples to scroll a nested gridview, I have an example of one, but doesn't work, I can't scroll it, im doing something wrong?
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["Search"] = "All";
ViewState["Search1"] = "All";
BindName();
BindTotal();
gvStudents.DataSource = GetData("select * from Student");
gvStudents.DataBind();
}
}
private void BindTotal()
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-A3KJHD1\SQLEXPRESS;Initial Catalog=aptiv;Integrated Security=SSPI");
DataTable dt1 = new DataTable();
SqlDataAdapter da1 = new SqlDataAdapter();
SqlCommand cmd1 = new SqlCommand("GetTotal");
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@Search", ViewState["Search1"].ToString());
cmd1.Connection = con;
da1.SelectCommand = cmd1;
da1.Fill(dt1);
gvStudents.DataSource = dt1;
gvStudents.DataBind();
DropDownList ddlTotal =
(DropDownList)gvStudents.HeaderRow.FindControl("ddlTotal");
this.BindTotalList(ddlTotal);
}
private void BindName()
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-A3KJHD1\SQLEXPRESS;Initial Catalog=aptiv;Integrated Security=SSPI");
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("GetName");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Search", ViewState["Search"].ToString());
cmd.Connection = con;
da.SelectCommand = cmd;
da.Fill(dt);
gvStudents.DataSource = dt;
gvStudents.DataBind();
DropDownList ddlName =
(DropDownList)gvStudents.HeaderRow.FindControl("ddlName");
this.BindNameList(ddlName);
}
private void BindNameList(DropDownList ddlName)
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-A3KJHD1\SQLEXPRESS;Initial Catalog=aptiv;Integrated Security=SSPI");
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("select * from Student");
cmd.Connection = con;
con.Open();
ddlName.DataSource = cmd.ExecuteReader();
ddlName.DataTextField = "Name";
ddlName.DataValueField = "Name";
ddlName.DataBind();
con.Close();
ddlName.Items.FindByValue(ViewState["Search"].ToString())
.Selected = true;
}
private void BindTotalList(DropDownList ddlTotal)
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-A3KJHD1\SQLEXPRESS;Initial Catalog=aptiv;Integrated Security=SSPI");
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("select * from Student");
cmd.Connection = con;
con.Open();
ddlTotal.DataSource = cmd.ExecuteReader();
ddlTotal.DataTextField = "Total";
ddlTotal.DataValueField = "Total";
ddlTotal.DataBind();
con.Close();
ddlTotal.Items.FindByValue(ViewState["Search1"].ToString())
.Selected = true;
}
protected void NameChanged(object sender, EventArgs e)
{
DropDownList ddlName = (DropDownList)sender;
ViewState["Search"] = ddlName.SelectedValue;
this.BindName();
}
protected void TotalChanged(object sender, EventArgs e)
{
DropDownList ddlTotal = (DropDownList)sender;
ViewState["Search1"] = ddlTotal.SelectedValue;
this.BindTotal();
}
private static DataTable GetData(string query)
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-A3KJHD1\SQLEXPRESS;Initial Catalog=aptiv;Integrated Security=SSPI");
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = query;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string StudentId = gvStudents.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvDetails = e.Row.FindControl("gvDetails") as GridView;
gvDetails.DataSource = GetData(string.Format("select * from Student where id='" + StudentId + "'"));
gvDetails.DataBind();
}
}
JS:
$("[src*=Up]").live("click", function () {
$(this).closest("tr").after("<tr><td></td><td colspan = '1000'>" + $(this).next().html() + "</td></tr>")
$(this).attr("src", "images/Down.jpg");
});
$("[src*=Down]").live("click", function () {
$(this).attr("src", "images/Up.jpg");
$(this).closest("tr").next().remove();
});
HTML:
<asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="false"
DataKeyNames="id" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt = "" style="height:10px;" src="images/Up.jpg" />
<asp:Panel ID="pnlDetails" runat="server" Style="display: none">
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="id" HeaderText="id" />
<asp:BoundField ItemStyle-Width="150px" DataField="age" HeaderText="age" />
<asp:BoundField ItemStyle-Width="150px" DataField="Class" HeaderText="Class" />
<asp:BoundField ItemStyle-Width="150px" DataField="s_Address" HeaderText="Address" />
<asp:BoundField ItemStyle-Width="150px" DataField="Phone" HeaderText="Phone" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Name:
<asp:DropDownList ID="ddlName" runat="server"
OnSelectedIndexChanged = "NameChanged" AutoPostBack = "true"
AppendDataBoundItems = "true">
<asp:ListItem Text = "Select" Value = "Select"></asp:ListItem>
<asp:ListItem Text = "All" Value = "All"></asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Total:
<asp:DropDownList ID="ddlTotal" runat="server"
OnSelectedIndexChanged = "TotalChanged" AutoPostBack = "true"
AppendDataBoundItems = "true">
<asp:ListItem Text = "Select" Value = "Select"></asp:ListItem>
<asp:ListItem Text = "All" Value = "All"></asp:ListItem>
<asp:ListItem Text = "Greater Than 90" Value = "90"></asp:ListItem>
<asp:ListItem Text = "Less Than 30" Value = "30"></asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<%# Eval("Total") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</form>





