Solved

I cant get a Gridview to work

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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