?
Solved

I cant get a Gridview to work

Posted on 2009-05-11
1
Medium Priority
?
229 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
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…
Suggested Courses

765 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