Solved

Maka a dynamic sortable Gridview from DataTable

Posted on 2006-07-13
9
1,407 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

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 …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

627 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