Compdigitec Labs

« | Home | »

ASP.NET GridView All-In-One Quick Reference

By admin | July 17, 2010

Here is a quick collection of snippets where one can quickly lookup the necessities of setting up a GridView for the purposes of displaying information to the viewer through a database (for example, MySQL):

Adding Columns of Data

DataTable dt = new DataTable("Tablename");
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
dt.Columns.Add("Column3");
// for every row
DataRow dr = dt.NewRow();
dr["Column1"] = "Testing";
dr["Column2"] = "Blah";
dr["Column3"] = "Big Blah";
dt.Rows.Add(dr);

Binding the Data (and storage across sessions)

DataView dv = new DataView(dt);
Session["data"] = dv;
gridviewctrl.DataSource = dv;
gridviewctrl.DataBind();

Paging
Note: This needs AllowPaging to be set to “true”. The following code goes in the “PageIndexChanging” callback.

gridviewctrl.PageIndex = e.NewPageIndex;
gridviewctrl.DataSource = Session["data"];
gridviewctrl.DataBind();

Sorting
Note: This needs AllowSorting to be set to “true” and ensure all columns you want sorted have a “SortExpression” that matches the column name in the DataTable. The following code goes in the “Sorting” callback.

string dir;
if (e.SortDirection == SortDirection.Ascending) {
    dir = "ASC";
} else {
    dir = "DESC";
}
((DataView)Session["Data"]).Sort = e.SortExpression + " " + dir;
gridviewctrl.DataSource = Session["data"];
gridviewctrl.DataBind();

<asp:ButtonField>s
Create a <asp:ButtonField> declaration as follows: <asp:ButtonField ButtonType=”Button” Text=”Button Text Here” CommandName=”DoSomething” />

if (e.CommandName == "DoSomething") {
    // get Index
    int index = Convert.ToInt32(e.CommandArgument);
    string test = ((DataView)Session["Data"]).Table.Rows[index]["Column1"];
    Response.Redirect("http://www.google.com/search?q=" + test.ToString()");
}

If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: Internet, Windows | 11 Comments »

11 Responses to “ASP.NET GridView All-In-One Quick Reference”

  1. Larry Says:
    November 2nd, 2024 at 10:49

    … [Trackback]

    […] Find More on on that Topic: compdigitec.com/labs/2010/07/17/asp-net-gridview-all-in-one-quick-reference/ […]

  2. ezybet Says:
    November 12th, 2024 at 02:52

    … [Trackback]

    […] Information to that Topic: compdigitec.com/labs/2010/07/17/asp-net-gridview-all-in-one-quick-reference/ […]

  3. ช่างกุญแจเพชรบุรี Says:
    November 12th, 2024 at 20:22

    … [Trackback]

    […] There you can find 69798 additional Information on that Topic: compdigitec.com/labs/2010/07/17/asp-net-gridview-all-in-one-quick-reference/ […]

  4. เว็บพนันแทงบอล ออนไลน์ lsm99day Says:
    November 15th, 2024 at 22:56

    … [Trackback]

    […] Read More to that Topic: compdigitec.com/labs/2010/07/17/asp-net-gridview-all-in-one-quick-reference/ […]

  5. KC9 Says:
    November 20th, 2024 at 20:16

    … [Trackback]

    […] Read More on that Topic: compdigitec.com/labs/2010/07/17/asp-net-gridview-all-in-one-quick-reference/ […]

  6. slot99 Says:
    November 24th, 2024 at 19:56

    … [Trackback]

    […] There you can find 81386 more Information to that Topic: compdigitec.com/labs/2010/07/17/asp-net-gridview-all-in-one-quick-reference/ […]

  7. ทางเข้าpg Says:
    November 27th, 2024 at 21:41

    … [Trackback]

    […] Read More Information here to that Topic: compdigitec.com/labs/2010/07/17/asp-net-gridview-all-in-one-quick-reference/ […]

  8. bangkok tattoo Says:
    November 28th, 2024 at 21:23

    … [Trackback]

    […] Read More Information here on that Topic: compdigitec.com/labs/2010/07/17/asp-net-gridview-all-in-one-quick-reference/ […]

  9. Sevink Molen Says:
    December 4th, 2024 at 14:03

    … [Trackback]

    […] Info to that Topic: compdigitec.com/labs/2010/07/17/asp-net-gridview-all-in-one-quick-reference/ […]

  10. SHOPEE สล็อต Says:
    December 13th, 2024 at 19:54

    … [Trackback]

    […] Find More to that Topic: compdigitec.com/labs/2010/07/17/asp-net-gridview-all-in-one-quick-reference/ […]

  11. PG SLOT เกมยอดฮิตแตกง่าย Says:
    December 19th, 2024 at 00:18

    … [Trackback]

    […] Read More to that Topic: compdigitec.com/labs/2010/07/17/asp-net-gridview-all-in-one-quick-reference/ […]

Comments