Solved

Maka a dynamic sortable Gridview from DataTable

Posted on 2006-07-13
9
1,401 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

18 Experts available now in Live!

Get 1:1 Help Now