3

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
  • Yup, prior to your FooterStyle, you need to add specific BoundFields for the fields you want to show, along with the TemplateField. See my answer for an updated edit. Commented May 23, 2009 at 19:55

1 Answer 1

4

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>
Sign up to request clarification or add additional context in comments.

10 Comments

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?
Define the order by adding BoundFields and put your TemplateField where you like it.
@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
@rAyt: I guess your mentioning the samething which mannish had mentioned previously?
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.