locked
Assign DataSourceObject as datasource to Dynamically created gridviews RRS feed

  • Question

  • User-510697852 posted

    Hello

    I am using VS2015 and C#

    In my web app i am creating few gridviews dynamically. No of gridviews created at run time depends on data (so can be 1 or 2 or any no)

    So it's in loop. I want to assign DataSourceObject as datasource to each grid view.

    like,

    i=1;
    
    DataRow Myrow;
    
    foreach (DataRow row in dtSection.Rows)
    
    {
    
    Myrow = row;
    
    GridView grid = new GridView();
    
    grid.id = "grid" + i;
    
    grid.datasource = "data" + i;
    
    ObjectDataSource dataSource = new ObjectDataSource();
    dataSource.ID = "data" + i;
    dataSource.SelectMethod = "GetTable";
    dataSource.TypeName = "MyNameSpace.MyClass";
    
    i = i + 1;
    
    }
    
    
    
    // My GetTable method which returns datatable as datasource for each grid.
    
    public DataTable GetTable()
    {
    DataTable dtpage = new DataTable();
    dtpage = ReportDataTable.Clone();  // My another data table from which I am selecting few rows
    DataRow[] rowArray = ReportDataTable.Select("title = '" + Myrow[0].ToString() + "'");
    
    foreach (DataRow row1 in rowArray)
    {
    dtpage.ImportRow(row1);
    }
    
    return dtpage;
    }

    -----------------------------------

    For me its not working properly. GetTable method executing after the first loop finished i.e. after creating all grids. So When it finally executes method Myrow is null and it gives null exception.

    How to make it happen so it will assign correctly like,

    grid1.datasource = data1,  grid2.datasource = data2 .... etc

    Tuesday, September 5, 2017 2:03 AM

Answers

  • User-510697852 posted

    Solved it :)

    Added selectparameters to ObjectDataSource.

    Now its working for me.

    foreach (DataRow row in dtSection.Rows)
    {
    
        GridView grid = new GridView();
    
        grid.id = "grid" + i;
    
        grid.DataSourceID = "data" + i;
    
                ObjectDataSource dataSource = new ObjectDataSource();
    
                Parameter pm = new Parameter();
                pm.Type = TypeCode.String;
                pm.Name = "title";
                pm.DefaultValue = row[0].ToString();
    
                dataSource.ID = "data" + i;
                dataSource.SelectMethod = "GetTable";           
                dataSource.SelectParameters.Clear();
                dataSource.SelectParameters.Add(pm);
                dataSource.TypeName = "InternalAuditInspection.ManageGraphReport";
                dataSource.Select();
                dataSource.DataBind();
    
                i = i+1;
    
    }
            public DataTable GetTable(String title)
            {
                DataTable dtpage = new DataTable();
                dtpage = ReportDataTable.Clone();
                DataRow[] rowArray = ReportDataTable.Select("title ='"+ title + "'");
    
                foreach (DataRow row1 in rowArray)
                {
                    dtpage.ImportRow(row1);
                }
    
                return dtpage;
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 5, 2017 9:29 AM

All replies

  • User-707554951 posted

    Hi hardeshis,

    1.You need to set DataSourceID property of Grid, not DataSource

    2.Ensure that both controls ObjectDataSource and GridView are added on Page.

    Class1:

    using System.Data;
    using System.Linq;
    using System.Web;
    
    namespace Case_Test
    {
        public class Class1
        {
            public DataTable GetTable()
            {
    
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[2] {
                new DataColumn("Item", System.Type.GetType("System.String")),
                new DataColumn("Rmk_Dt", System.Type.GetType("System.DateTime"))
            });
                dt.Rows.Add("Shirt", DateTime.Now);
                dt.Rows.Add("Jeans", DateTime.Now.AddDays(8));
                dt.Rows.Add("Trousers", DateTime.Now.AddDays(19));
                dt.Rows.Add("Tie", DateTime.Now.AddDays(1));
                dt.Rows.Add("Cap", DateTime.Now.AddDays(-20));
                return dt;
            }
        }
    }

    CodeBehind:

           protected void Page_Load(object sender, System.EventArgs e)
            {
    // Because I don't know 'dtSection' is what, so I commented some code line for test int i = 1; // DataRow Myrow; //foreach (DataRow row in dtSection.Rows) //{ //Myrow = row; GridView grid = new GridView(); grid.ID = "grid" + i; 1. grid.DataSourceID = "data" + i; ObjectDataSource dataSource = new ObjectDataSource(); dataSource.ID = "data" + i; dataSource.SelectMethod = "GetTable"; 2. this.form1.Controls.Add(grid); this.form1.Controls.Add(dataSource); dataSource.TypeName = " Case_Test.Class1"; //i = i + 1; //} }

    Output:

    Best regards 

    Cathy

    Tuesday, September 5, 2017 7:32 AM
  • User-510697852 posted

    Yes cathy it's datasourceid. By mistake i gave datasource.

    But now i solved it.

    Hi hardeshis,

    1.You need to set DataSourceID property of Grid, not DataSource

    2.Ensure that both controls ObjectDataSource and GridView are added on Page.

    Class1:

    using System.Data;
    using System.Linq;
    using System.Web;
    
    namespace Case_Test
    {
        public class Class1
        {
            public DataTable GetTable()
            {
    
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[2] {
                new DataColumn("Item", System.Type.GetType("System.String")),
                new DataColumn("Rmk_Dt", System.Type.GetType("System.DateTime"))
            });
                dt.Rows.Add("Shirt", DateTime.Now);
                dt.Rows.Add("Jeans", DateTime.Now.AddDays(8));
                dt.Rows.Add("Trousers", DateTime.Now.AddDays(19));
                dt.Rows.Add("Tie", DateTime.Now.AddDays(1));
                dt.Rows.Add("Cap", DateTime.Now.AddDays(-20));
                return dt;
            }
        }
    }

    CodeBehind:

           protected void Page_Load(object sender, System.EventArgs e)
            {
    // Because I don't know 'dtSection' is what, so I commented some code line for test int i = 1; // DataRow Myrow; //foreach (DataRow row in dtSection.Rows) //{ //Myrow = row; GridView grid = new GridView(); grid.ID = "grid" + i; 1. grid.DataSourceID = "data" + i; ObjectDataSource dataSource = new ObjectDataSource(); dataSource.ID = "data" + i; dataSource.SelectMethod = "GetTable"; 2. this.form1.Controls.Add(grid); this.form1.Controls.Add(dataSource); dataSource.TypeName = " Case_Test.Class1"; //i = i + 1; //} }

    Output:

    Best regards 

    Cathy

    Yes it's datasourceid. Datasource i gave by mistake. But very much thank you for trying. :) 

    Sorry for posting wrong code.

    But I solved it.

    Tuesday, September 5, 2017 9:26 AM
  • User-510697852 posted

    Solved it :)

    Added selectparameters to ObjectDataSource.

    Now its working for me.

    foreach (DataRow row in dtSection.Rows)
    {
    
        GridView grid = new GridView();
    
        grid.id = "grid" + i;
    
        grid.DataSourceID = "data" + i;
    
                ObjectDataSource dataSource = new ObjectDataSource();
    
                Parameter pm = new Parameter();
                pm.Type = TypeCode.String;
                pm.Name = "title";
                pm.DefaultValue = row[0].ToString();
    
                dataSource.ID = "data" + i;
                dataSource.SelectMethod = "GetTable";           
                dataSource.SelectParameters.Clear();
                dataSource.SelectParameters.Add(pm);
                dataSource.TypeName = "InternalAuditInspection.ManageGraphReport";
                dataSource.Select();
                dataSource.DataBind();
    
                i = i+1;
    
    }
            public DataTable GetTable(String title)
            {
                DataTable dtpage = new DataTable();
                dtpage = ReportDataTable.Clone();
                DataRow[] rowArray = ReportDataTable.Select("title ='"+ title + "'");
    
                foreach (DataRow row1 in rowArray)
                {
                    dtpage.ImportRow(row1);
                }
    
                return dtpage;
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 5, 2017 9:29 AM