tags:

views:

6339

answers:

1

I'm working on a shopping cart in a website and I have my items(which have been added to the cart) in an arraylist. And using these values I've read the rest of the values from the db and and have populated on the gridview successfully.

        if (d1.Read())
        {
            d1.Close();
            sda.SelectCommand = searchResult;
            sda.Fill(dt);
            GridView2.DataSource = dt;
            GridView2.DataBind();
        }

How can I add an additional column named "Quantity" in this gridview along with a text box so that user can enter a value for the qunatity for each item displayed in the gridview?

Any help/suggestions/links would be greatly appreciated.

thanks,

//edit 2

<asp:GridView ID="GridView2" runat="server" BackColor="#388AD0"
    BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
    GridLines="Vertical" Height="160px" Style="left: 72px;
    position: relative; top: 8px" Width="504px" AllowPaging="True" PageSize="5">
    <FooterStyle BackColor="#E0E0E0" />
    <PagerStyle BackColor="Silver" ForeColor="Black" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#388AD0" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="#388AD0" />
    <PagerSettings Mode="NextPrevious" />
</asp:GridView>

//edit 3

public void additem(string additem) 
{
    DataTable dt = new DataTable();
    string ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ewap_k.mdf;Integrated Security=True;User Instance=True";
    using (SqlConnection searchCon = new SqlConnection(ConnectionString))
    {
        using (SqlCommand searchResult = new SqlCommand("SELECT ItemID, Name, RelDate, Price, Status FROM item_k WHERE ItemID IN ( " + itemIDs + ")", searchCon))
        {
            searchCon.Open();
            SqlDataReader d1 = searchResult.ExecuteReader();
            SqlDataAdapter sda = new SqlDataAdapter();

            if (d1.Read())
            {
                d1.Close();
                sda.SelectCommand = searchResult;
                sda.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            else { }
            searchCon.Close();
        }
    }
}

//edit 4

<asp:GridView ID="GridView1" runat="server" Style="left: 8px; position: relative;
    top: 0px">
    <Columns>
        <asp:BoundField DataField="ItemID"  Visible="false"/>
        <asp:BoundField DataField="Name" />
        <asp:BoundField DataField="RelDate" />
        <asp:TemplateField>
        <ItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
        <asp:BoundField DataField="Price" />
        <asp:BoundField DataField="Status" />
    </Columns>
</asp:GridView>
+1  A: 

You can add a template field and drop the textbox in the ItemTemplate as such:

<asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>

[Edit] Here's a more complete grid:

<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:BoundField />
        <asp:BoundField />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Notice that the TemplateField shows up wherever I decide to put it in the list of columns. If that still isn't working for you, can you post your GridView markup so I can see what you've got?

[Edit 2] Here's an updated version of your grid. You'll just need to set up the BoundFields to bind to whichever value in your dataset that you need.

<asp:GridView ID="GridView2" runat="server" BackColor="#388AD0" BorderColor="Black"
    BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical"
    Height="160px" Style="left: 72px; position: relative; top: 8px" Width="504px"
    AllowPaging="True" PageSize="5">
    <Columns>
        <asp:BoundField />
        <asp:BoundField />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#E0E0E0" />
    <PagerStyle BackColor="Silver" ForeColor="Black" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#388AD0" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="#388AD0" />
    <PagerSettings Mode="NextPrevious" />
</asp:GridView>
mannish
thanks, I tried that and it worked. This new column appears as the 1st column. How can I put it between the columns I want?
pier
Define the order by adding BoundFields and put your TemplateField where you like it.
Henrik P. Hessel
@mannish: I tried your code, and that would just create 2 columns infront of the column with textbox. Remember the values I want the gridview to display are taken from the db
pier
@rAyt: I guess your mentioning the samething which mannish had mentioned previously?
pier
You'll still get the values from the database, but you have to specifically bind the values from your dataset rather than allow it to autobind the columns. If you're auto binding your dataset in your code behind, you might be able to dynamically add the template column after the grid has been populated, but I think that's the more difficult/buggy route. It would be better to specify the columns and values you want ahead of time which gives you greater control over what goes where.
mannish
This site has some decent working examples of different ways to setup a GridView: http://quickstarts.asp.net/quickstartv20/aspnet/doc/ctrlref/data/gridview.aspx. The second example down shows one possible way to specify the data you want for each boundfield. Note that they have specifically bound the grid by using the DataSourceID in the Grid markup. You don't have to do it this way, but it's one possible solution.
mannish
@mannish:do i have to set the datasource to dt? as mentioned in my c# code in asp.net?
pier
@mannish: I set the boundfields to the db values, now I get "ItemID, Name, RelDate, Price, Status" + another set of "ItemID, Name, RelDate, Price, Status". All together gridview now displays 10 columns. I'm confused.
pier
I'm not sure what you mean by "set the datasource to dt", but presumably you can do the data binding a number of different of ways. If you create a datasource on the page and set the DataSourceID of the GridView to the ID of the datasource, that would work. You can also still do the databinding in the code behind, but if you do, I think you have to specify the column values a little differently. Can you show how your databinding is setup right now? Either from the page itself (similar to the examples in the link I mentioned above) or from the code behind?
mannish
pier
Yeah, you're getting the second set of columns because the grid is still trying to auto-generate columns. There's an attribute on GridView that allows you to turn off that feature, which leaves the responsibility of building the columns solely in your hands. You'll want to add AutoGenerateColumns="false" to your GridView. In terms of binding, what you have should work.
mannish
It worked!! Thanks! :)
pier