Solved

Maka a dynamic sortable Gridview from DataTable

Posted on 2006-07-13
9
1,404 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…

829 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