locked
DataTable in DataGridView RRS feed

  • Question

  • User328483283 posted

    I am using DataTable as a data source in DataGridView. I am using Row header from Item Template in GridView. But when i add a record, it adds both records from Item Template as well as from DataTable. So i have two records in one line in Gridview. How i can hide DataTable records and show only item template results......

    <asp:GridView ID="PurchaseGV" runat="server" CellPadding="4" 
    CssClass="inner_rail_horizontal" ForeColor="#333333" GridLines="None"
    ShowFooter="True" Width="100px" DataSourceID="ObjectDataSource1">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
    <asp:TemplateField HeaderText="Product">
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<% #Eval("Product") %>'> </asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Serial No">
    <EditItemTemplate>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                                    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text='<% #Eval("Serial No") %>' ></asp:Label>
                                    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Quantity">
    <EditItemTemplate>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                                    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label3" runat="server" Text='<% #Eval("Quantity") %>' ></asp:Label>
                                    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Price">
    <EditItemTemplate>
    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                                    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label4" runat="server" Text='<% #Eval("Price") %>' ></asp:Label>
                                    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    
    
    Code Behind File
    
    
        DataTable dt;

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    dt = new DataTable();
    MakeTable();
    Session["myTable"] = dt;
    }
    }
    public void MakeTable()
    {
    dt.Columns.Add("Product");
    dt.Columns.Add("Serial No");
    dt.Columns.Add("Quantity");
    dt.Columns.Add("Discount");
    }
    public void AddToTable()
    {
    dt = (DataTable)Session["myTable"];
    DataRow dr = dt.NewRow();
    dr["Product"] = ProductTextBox.Text.ToString();
    dr["Serial No"] = SerialTextBox.Text.ToString();
    dr["Quantity"] = QuantityTextBox.Text.ToString();
    dr["Discount"] = DiscountTextBox.Text.ToString();
    dt.Rows.Add(dr);
    Session["myTable"] = dt;
    }
    public void BindGrid()
    {
    PurchaseGV.DataSource = dt;
    PurchaseGV.DataBind();
    }

    public void MyButton_Click(object sender, EventArgs e)
    {
    AddToTable();
    BindGrid();
    }
    Saturday, August 20, 2011 7:44 AM

Answers

  • User-359936451 posted

    Set the property of the Gridview, AutoGenerateColums = false.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 20, 2011 12:13 PM