locked
Problem Iterating a GridView RRS feed

  • Question

  • User1043796333 posted
    I have the following piece of code:
     
        protected void Button1_Click(object sender, EventArgs e)
        {
            DataSourceSelectArguments dsaArgs = new DataSourceSelectArguments();
            DataView view = (DataView)SqlDataSource1.Select(dsaArgs);
            DataTable dt = view.ToTable();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    string s = dt.Rows[i][j].ToString();
                }
            }      
        }
    I'd like to make it work too for an EntityDataSource but I haven't been able of finding an equivlent sentence to:
    DataView view = (DataView)SqlDataSource1.Select(dsaArgs);
    Any help with this will be very welcomed
    Thursday, July 14, 2011 1:32 PM

Answers

  • User-843484705 posted
    Convert IList into DataTable
    /// <summary>
    /// Default Constructor
    /// </summary>
    private GenericToDataTable()
    { }
    /// <summary>
    ///
    /// </summary>
    /// <typeparam name=”T”>Custome Class </typeparam>
    /// <param name=”lst”>List Of The Custome Class</param>
    /// <returns> Return the class datatbl </returns>
    public static DataTable ConvertTo<T>(IList<T> lst)
    {
    //create DataTable Structure
    DataTable tbl = CreateTable<T>();
    Type entType = typeof(T);

    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
    //get the list item and add into the list
    foreach (T item in lst)
    {
    DataRow row = tbl.NewRow();
    foreach (PropertyDescriptor prop in properties)
    {
    row[prop.Name] = prop.GetValue(item);
    }
    tbl.Rows.Add(row);
    }

    return tbl;
    }

    /// <summary>
    ///
    /// </summary>
    /// <typeparam name=”T”>Custome Class</typeparam>
    /// <returns></returns>
    public static DataTable CreateTable<T>()
    {
    //T –> ClassName
    Type entType = typeof(T);
    //set the datatable name as class name
    DataTable tbl = new DataTable(entType.Name);
    //get the property list
    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
    foreach (PropertyDescriptor prop in properties)
    {
    //add property as column
    tbl.Columns.Add(prop.Name, prop.PropertyType);
    }
    return tbl;
    }
    }
    http://forums.asp.net/t/1065708.aspx/1
    Video: http://msdn.microsoft.com/en-us/data/Video/cc546554
    http://dotnetstories.wordpress.com/2009/09/27/asp-net-4-0-entity-datasource-and-gridview/
    http://msdn.microsoft.com/en-us/library/cc488502.aspx
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.entitydatasource.aspx
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 14, 2011 1:44 PM

All replies

  • User-843484705 posted
    Convert IList into DataTable
    /// <summary>
    /// Default Constructor
    /// </summary>
    private GenericToDataTable()
    { }
    /// <summary>
    ///
    /// </summary>
    /// <typeparam name=”T”>Custome Class </typeparam>
    /// <param name=”lst”>List Of The Custome Class</param>
    /// <returns> Return the class datatbl </returns>
    public static DataTable ConvertTo<T>(IList<T> lst)
    {
    //create DataTable Structure
    DataTable tbl = CreateTable<T>();
    Type entType = typeof(T);

    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
    //get the list item and add into the list
    foreach (T item in lst)
    {
    DataRow row = tbl.NewRow();
    foreach (PropertyDescriptor prop in properties)
    {
    row[prop.Name] = prop.GetValue(item);
    }
    tbl.Rows.Add(row);
    }

    return tbl;
    }

    /// <summary>
    ///
    /// </summary>
    /// <typeparam name=”T”>Custome Class</typeparam>
    /// <returns></returns>
    public static DataTable CreateTable<T>()
    {
    //T –> ClassName
    Type entType = typeof(T);
    //set the datatable name as class name
    DataTable tbl = new DataTable(entType.Name);
    //get the property list
    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
    foreach (PropertyDescriptor prop in properties)
    {
    //add property as column
    tbl.Columns.Add(prop.Name, prop.PropertyType);
    }
    return tbl;
    }
    }
    http://forums.asp.net/t/1065708.aspx/1
    Video: http://msdn.microsoft.com/en-us/data/Video/cc546554
    http://dotnetstories.wordpress.com/2009/09/27/asp-net-4-0-entity-datasource-and-gridview/
    http://msdn.microsoft.com/en-us/library/cc488502.aspx
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.entitydatasource.aspx
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 14, 2011 1:44 PM
  • User1043796333 posted

    Thanks for your help

    I'm not quite sure I understand your answer so please forgive me for asking you this:

    The code you've provided is the equivalente to the one I pasted in my post or does your code works as a complement of it?

    Which is the way to make your code functioning? How can I implement it: Let' say I have an .aspx page with a GridView only with its corresponding EntityDataSource

    As you can see I'm kind of lost here

    Thursday, July 14, 2011 2:09 PM
  • User1043796333 posted

    Thanks

    Thursday, July 14, 2011 8:20 PM