Solved

Maka a dynamic sortable Gridview from DataTable

Posted on 2006-07-13
9
1,403 Views
Last Modified: 2012-06-27
Hi! I got a method returning a DataTable. What I want to do is call this method from my codebehindfile and use this Datatable as input for a dynamic GridView. I also need to be able to sort this GridView by columnName. Anyone know how to do this in c#?
0
Comment
Question by:frodehoe
9 Comments
 
LVL 9

Expert Comment

by:zaghaghi
ID: 17097378
see this:

// you know how to get data table
DataTable dt = getDataTable();

//set the data source of grid view
gridView1.DataSource = dt;
// or for example
dt.Tables[0].DefaultView.Sort = "Name";
gridView1.DataSource = dt.Tables[0].DefaultView;


// bind data to grid view
gridView1.DataBind();


0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 17097397
Hi

Check out this Link
http://www.codeproject.com/aspnet/elphGrid.asp


Hope This will Help U


Cheers!!!
0
 

Author Comment

by:frodehoe
ID: 17097548
Hi! Thanks for the quick response. Cant get your code to work zaghagi. Here's my code thats showing the gridview (but nnothing happens when I try to sort it)

public partial class _Default : System.Web.UI.Page
{
    string fornavn;
    string etternavn;
    string tittel;
    string selskap;
    string sted;
    string mobil =;

    protected void Page_Load(object sender, EventArgs e)
    {
       
       // loadDynamicGrid();
       
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable m_DataTable = GrdDynamic.DataSource as DataTable;

        if (m_DataTable != null)
        {
            DataView m_DataView = new DataView(m_DataTable);
            m_DataView.Sort = e.SortExpression;

            GrdDynamic.DataSource = m_DataView;
            GrdDynamic.DataBind();
        }


    }
    protected void GridView1_Paging(object sender, GridViewPageEventArgs e)
    {
        GrdDynamic.PageIndex = e.NewPageIndex;
        GrdDynamic.DataBind();
    }
   

    private void loadDynamicGrid()
    {

        #region Code for preparing the DataTable
        fornavn = TextBox1.Text;
        etternavn = TextBox2.Text;
        tittel = TextBox3.Text;
        selskap = DropDownList2.Text;
        sted = DropDownList1.Text;
        mobil = TextBox4.Text;
        ADSearch ad = new ADSearch(fornavn,etternavn, tittel, selskap, sted, mobil);
        DataTable dat = new DataTable();
        dat = ad.doQuery();
       
       
        //Iterate through the columns of the datatable to set the data bound field dynamically.
        foreach (DataColumn col in dat.Columns)
        {
            //Declare the bound field and allocate memory for the bound field.
            BoundField bfield = new BoundField();

            //Initalize the DataField value.
            bfield.DataField = col.ColumnName;

            //Initialize the HeaderText field value.
            bfield.HeaderText = col.ColumnName;
            bfield.SortExpression = col.ColumnName;
           
            //Add the newly created bound field to the GridView.
            GrdDynamic.Columns.Add(bfield);

            //Initialize the DataSource
            GrdDynamic.DataSource = dat;

            //Bind the datatable with the GridView.
            GrdDynamic.DataBind();
        }
       
         
        #endregion
    }
       
    protected void SearchBtn_Click(object sender, EventArgs e)
    {
        loadDynamicGrid();
    }
   
}


<asp:GridView ID="GrdDynamic" AllowPaging="True" OnPageIndexChanging="GridView1_Paging" AllowSorting="True" OnSorting="GridView1_Sorting" runat="server" AutoGenerateColumns="False"  >
                    </asp:GridView>
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:frodehoe
ID: 17097896
Anyone know what I'm doing wrong here?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17098993
Is this case being satisfied in the Sorting handler?

   if (m_DataTable != null)

Bob
0
 

Author Comment

by:frodehoe
ID: 17106438
I solved the problem by using ObjecDataSource. Thanks for all your help
Heres the code I got to work:

<asp:GridView ID="GrdDynamic" AllowPaging="False" AllowSorting="True"  runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" >
                    </asp:GridView>
                    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="doQuery"
                        TypeName="ADSearch">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="TextBox1" DefaultValue="" Name="fNavn" PropertyName="Text"
                                Type="String" />
                            <asp:ControlParameter ControlID="TextBox2" Name="eNavn" PropertyName="Text" Type="String" />
                            <asp:ControlParameter ControlID="TextBox3" Name="tittel" PropertyName="Text" Type="String" />
                            <asp:ControlParameter ControlID="DropDownList2" Name="selskap" PropertyName="SelectedValue"
                                Type="String" />
                            <asp:ControlParameter ControlID="DropDownList1" Name="sted" PropertyName="SelectedValue"
                                Type="String" />
                            <asp:ControlParameter ControlID="TextBox4" Name="mobil" PropertyName="Text" Type="String" />
                        </SelectParameters>
                    </asp:ObjectDataSource>

And then I removed the binding in my codebehind file
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 17489541
PAQ
ee ai construct, community support moderator
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now