Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

I cant get a Gridview to work

Posted on 2009-05-11
1
Medium Priority
?
230 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
[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
1 Comment
 
LVL 9

Accepted Solution

by:
tillgeffken earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 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