Programatically changing Gridview settings in a nested Gridview?

Last post 01-03-2009 7:05 AM by ladyath. 4 replies.

Sort Posts:

  • Programatically changing Gridview settings in a nested Gridview?

    12-30-2008, 7:56 AM
    • Member
      21 point Member
    • ladyath
    • Member since 02-19-2008, 7:40 AM
    • Posts 72

    Hi

    Thanks to help from the forum, my nested gridview now looks like this:

    Index.aspx
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ERMINFINITYConnectionString %>" 
            SelectCommand="SELECT CI_CriticalIssues.issueid, CI_CriticalIssues.criticalissue, CI_CriticalIssues.issueowner, CI_Results.resultid, CI_Results.issueid AS Expr1, CI_Results.result, CI_Steps.stepid, CI_Steps.resultid AS Expr2, CI_Steps.step, CI_Steps.status, CI_Steps.stepdue, CI_Steps.review FROM CI_CriticalIssues INNER JOIN CI_Results ON CI_CriticalIssues.issueid = CI_Results.issueid INNER JOIN CI_Steps ON CI_Results.resultid = CI_Steps.resultid"></asp:SqlDataSource>
        
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            OnRowDataBound="GridView1_RowDataBound" DataKeyNames="issueid" >
                <Columns>
                    <asp:BoundField DataField="criticalissue" HeaderText="Critical Issue"/>
                    <asp:BoundField DataField="issueowner" HeaderText="Issue Owner"/>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:GridView ID="childGV1" runat="server" DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("relation12") %>' AutoGenerateColumns="false" 
                                    OnRowDataBound="childGV1_rowDataBound" >
                                <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
    		    <asp:Label ID="label11" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "[\"resultid\"]")%>'></asp:Label>           
                                        <%# DataBinder.Eval(Container.DataItem, "[\"issueid\"]")%>    
                                        <%# DataBinder.Eval(Container.DataItem, "[\"result\"]")%>  
    		</ItemTemplate>
                                   </asp:TemplateField>                               
                                   <asp:TemplateField>
                                    <ItemTemplate>
                                       <asp:GridView ID="childGV2" runat="server" AutoGenerateColumns="true" >
                                         <Columns>
                                                <asp:TemplateField>
                                                    <ItemTemplate>                                                
                                                        <%# DataBinder.Eval(Container.DataItem, "[\"stepid\"]") %>   
                                                        <%# DataBinder.Eval(Container.DataItem, "[\"resultid\"]") %>   
                                                        <%# DataBinder.Eval(Container.DataItem, "[\"step\"]") %>   
                                                        <%# DataBinder.Eval(Container.DataItem, "[\"status\"]") %>                                                       
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                            </Columns>
                                        </asp:GridView>
                                    </ItemTemplate>
                                   </asp:TemplateField>                               
                                </Columns>
                            </asp:GridView>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>        
            </asp:GridView>
      
    Index.aspx.cs
    public partial class Index : System.Web.UI.Page
    {
        DataSet ds = new DataSet();
        static string connstr = ConfigurationManager.ConnectionStrings["ERMINFINITYConnectionString"].ToString();
        SqlConnection con = new SqlConnection(connstr);
        public int count = 0;
        public int cnt = 0;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindSet();
        }
    
        public void BindSet()
        {
            SqlDataAdapter cmd1 = new SqlDataAdapter("select * from CI_CriticalIssues", con);
            DataSet ds = new DataSet();
            cmd1.Fill(ds, "exam1_k");
            count = ds.Tables["exam1_k"].Rows.Count;
    
            SqlDataAdapter cmd2 = new SqlDataAdapter("select * from CI_Results", con);
            cmd2.Fill(ds, "exam2_k");
            cnt = ds.Tables["exam2_k"].Rows.Count;
    
            SqlDataAdapter cmd3 = new SqlDataAdapter("select stepid, resultid, step, status from CI_Steps", con);
            cmd3.Fill(ds, "exam3_k");
    
            ds.Relations.Add("relation12", ds.Tables["exam1_k"].Columns["issueid"], ds.Tables["exam2_k"].Columns["issueid"]);
            ds.Relations.Add("relation23", ds.Tables["exam2_k"].Columns["resultid"], ds.Tables["exam3_k"].Columns["resultid"]);
    
    
            GridView1.DataSource = ds.Tables["exam1_k"];
            GridView1.DataBind();
            con.Close();
        }
    
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    
        { }
    
    
        protected void childGV1_rowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataSet ds = new DataSet();
                GridView gv12 = (GridView)e.Row.FindControl("childGV2");
                Label lblFind = (Label)e.Row.FindControl("label11");
                if (gv12 != null && lblFind != null)
                {
                    using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ERMINFINITYConnectionString"].ConnectionString))
                    {
                        using (SqlDataAdapter da = new SqlDataAdapter("select * from CI_Steps where resultid = " + Convert.ToInt32(lblFind.Text), con))
                        {
                            da.Fill(ds, "FinalTab");
                            if (ds.Tables["FinalTab"].Rows.Count > 0)
                            {
                                gv12.DataSource = ds.Tables["FinalTab"].DefaultView;
                                gv12.DataBind();
                            }
                        }
                    }
                }
            }
        }
    
        protected void OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].CssClass = "hiddencol";
            }
            else if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].CssClass = "hiddencol";
            }
        }
    }

     

    This displays a nested gridview containing a critical issue, the expected results and the steps to ensure the results are attained.  I need to hide the following columns, but still have the table use their values:  issueid, resultid and stepid. How do I do that?  I would also appreciate info on how I can add edit and update capabilities to the gridview to conform with the rest of the code.

    Please forgive the newbness, but I have exceedingly little experience with code.

  • Re: Programatically changing Gridview settings in a nested Gridview?

    01-02-2009, 3:05 AM

    Hi ladyath,

    Do you want to hide some columns in nested GridView? You can use HiddenField to bind data instead of current Label controls and direct bound text in GridView.

    If you want to implement edit in nested GridView, I think it will be same to what to do in outer GridView. Just add edit and update button, and handle RowEditing, RowUpdating events or RowCommand event.

    http://www.codeproject.com/KB/aspnet/EditNestedGridView.aspx

    Thanks,

    Qin Dian Tang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Programatically changing Gridview settings in a nested Gridview?

    01-02-2009, 7:23 AM
    • Member
      21 point Member
    • ladyath
    • Member since 02-19-2008, 7:40 AM
    • Posts 72

    Thanks for the response.  Could you please give me an example of how I can use the Hiddenfield in the code?  Also, does that not cause problems in using the hidden columns as part of the queries?  Since my nested gridview relies on the results of the fields I would like to hide from the user, surely it would also affect the query itself?  Or am I mistaken?

  • Re: Programatically changing Gridview settings in a nested Gridview?

    01-03-2009, 1:50 AM
    Answer
    • Participant
      1,070 point Participant
    • kuber.manral
    • Member since 02-27-2008, 5:20 AM
    • Posts 269

    hi,

       use hidden fields like this:

     <asp:HiddenField ID="hiddenquestionID" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "[\"questionid\"]") %>' />

    it is not available for user and still developer can use its values.

     

    Thanks & Regards
    Kuber Singh Manral.
    MCTS [ASP.Net 2.0 Web Client Application]

    Please "Mark As Answer", if any Post helps you. It facilitates to find
    exact solution of the problem.
  • Re: Programatically changing Gridview settings in a nested Gridview?

    01-03-2009, 7:05 AM
    • Member
      21 point Member
    • ladyath
    • Member since 02-19-2008, 7:40 AM
    • Posts 72

    Thank you!  :)

Page 1 of 1 (5 items)