Solved

I cant get a Gridview to work

Posted on 2009-05-11
1
223 Views
Last Modified: 2012-05-06
How do I make this to work?

I want to show my list but I cant make it to show up in my gridview. What do I miss?

Film  includes [0]
Artikelnr
Genre
Regissor
RentCost
Title



Code please.
<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SqlClient" %>

<%@ Import Namespace="System.Collections.Generic" %>

<%@ Import Namespace="ProjektFilm" %>
 

<html>

<head>

<title>ButtonField ButtonType Example</title>
 
 

<script language="C#" runat="server">

DataTable Cart;

DataView CartView;
 

void Page_Load(Object src, EventArgs e)

{

    if (!IsPostBack)

    {
 

        myGrid.DataSource = (List<Filminfo>)Session["film"];

        myGrid.DataBind();

    }
 

    if (Session["shoppingSession"] == null)

    {

        Cart = new DataTable();

        Cart.Columns.Add(new DataColumn("Item", typeof(string)));

        Cart.Columns.Add(new DataColumn("Price", typeof(string)));

        Session["shoppingSession"] = Cart;

    }

    else Cart = (DataTable)Session["shoppingSession"];
 

    CartView = new DataView(Cart);

    shopCart.DataSource = CartView;

    shopCart.DataBind();

}
 

void updateCart(Object src, GridViewCommandEventArgs e)

{

    DataRow dr = Cart.NewRow();
 

    // get the row index stored in the CommandArgument property

    int index = Convert.ToInt32(e.CommandArgument);
 

    // get the GridViewRow where the command is raised

    GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
 

    // for bound fields, values are stored in the Text property of Cells [ fieldIndex ]

    string item = selectedRow.Cells[1].Text;

    string price = selectedRow.Cells[2].Text;
 

    if (e.CommandName == "AddToCart")

    {

        dr[0] = item; dr[1] = price;

        Cart.Rows.Add(dr);

    }

    else

    {  // remove from Cart

        CartView.RowFilter = "Item='" + item + "'";

        if (CartView.Count > 0) CartView.Delete(0);

        CartView.RowFilter = "";

    }

    shopCart.DataBind();

}

   

</script>

</head>
 

<body>
 

<div class="header"><h3>ButtonField <span class="hilite">ButtonType</span> Example</h3></div>
 
 

<form id="Form1" runat="server">
 

<table cellpadding="5">

<tr valign="top">

   <td><b>Product List</b>
 

      <asp:gridview id="myGrid" runat="server"

         cellpadding=5 font-size="8pt"

         gridlines="horizontal"

         autogeneratecolumns=false         

         onRowCommand="updateCart">
 

         <headerstyle backcolor="darkslategray"

            forecolor="khaki" font-bold />
 

         <rowstyle backcolor="ivory" verticalalign="top" />
 

         <columns>
 
 
 

            <asp:buttonfield

               text=" + "

               buttontype="button"

               commandname="AddToCart"  />
 

            <asp:buttonfield

               text=" - "

               buttontype="button"

               commandname="RemoveFromCart" />
 

         </columns>
 

      </asp:gridview>

   </td>
 

   <td><b>Shopping Cart</b>
 

      <asp:gridview id="shopCart" runat="server"

         cellpadding=5 font-size="8pt"

         headerstyle-backcolor="darkslategray"

         headerstyle-forecolor="khaki"

         headerstyle-font-bold

         rowstyle-backcolor="ivory"

         rowstyle-verticalalign="top" />
 

   </td></tr>

</table>
 

</form>
 

<hr size=1 width=90%>
 

</body>

</html>

Open in new window

0
Comment
Question by:Mickeys
1 Comment
 
LVL 9

Accepted Solution

by:
tillgeffken earned 500 total points
ID: 24352423
Setting AutoGenerateColumns to false and not adding any columns besides your add and remove buttons usually leaves you with a gridview with only an add and a remove button. :)

Following example assumes there's a "Name" field in your datasource.

         <columns>

            <asp:buttonfield text=" + " buttontype="button" commandname="AddToCart"  />

            <asp:buttonfield text=" - " buttontype="button" commandname="RemoveFromCart" />

            <asp:boundfield datafield="Name" readonly="true" headertext="Name"/>

         </columns>

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

19 Experts available now in Live!

Get 1:1 Help Now