Solved

Maka a dynamic sortable Gridview from DataTable

Posted on 2006-07-13
9
1,406 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

759 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