Solved

I cant get a Gridview to work

Posted on 2009-05-11
1
227 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 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

724 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