0

I have a grid view with two columns. One named product and the other named values (both columns in template field).

The product column is bound to the "product" table. And the value field's item template contains a textbox.

I need to insert the values into database table "BrandProperties" through textbox in grid view, but I am not sure how to do this.

Here is my code:

    if (!IsPostBack)
    {
        BindView();

    }

    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter("select ID,TypeName from ProductTypes",con);
    da.Fill(dt);
    DropDownList1.DataSource = dt;
    DropDownList1.DataValueField = "ID";
    DropDownList1.DataTextField = "TypeName";
    DropDownList1.DataBind();
}

public void BindView()
{

    DataTable dt = new DataTable();
    string sql = "select * from Properties";
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter(sql, con);
    da.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    con.Close();
}

aspx.code:

   Text="Brand Name"></asp:Label>
    <asp:Button ID="Button1" runat="server" BackColor="#6699FF" 
        style="z-index: 1; left: 410px; top: 391px; position: absolute" 
        Text="SAVE" onclick="Button1_Click" />

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
    CellPadding="3" GridLines="Horizontal" 
    style="z-index: 1; left: 52px; top: 230px; position: absolute; height: 133px; width: 344px">
    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
    <Columns>
        <asp:TemplateField HeaderText="Product">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("PropertyName") %>' ></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Value">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
3
  • From footer template you can add new item. I have written a post on this Refer= satindersinght.blogspot.in/2012/08/… Commented May 21, 2013 at 11:11
  • but i need to add new item by using item template which contain a text box :( and that value should be saved in database Commented May 21, 2013 at 11:18
  • :Well as per my knowledge ItemTemplate is use to display data, for Update/ Editing EditTemplate used and for adding new item Footertemplate should be use . Here you mean to say in your case Item Template is available for editing ? user can view/edit Commented May 21, 2013 at 11:21

1 Answer 1

1

try this

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
    CellPadding="3" GridLines="Horizontal" Style="z-index: 1; left: 52px; top: 230px;
    position: absolute; height: 133px; width: 344px">
    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
    <Columns>
        <asp:TemplateField HeaderText="Product">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("PropertyName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Value">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                <asp:Button ID="btnSave" runat="server" Text="SaveValues" OnClick = "btnSave_OnClick" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>




protected void btnSave_OnClick(object sender, EventArgs e)
{
    Button lb = (Button)sender;
    GridViewRow row = (GridViewRow)lb.NamingContainer;
    if (row != null)
    {
        int index = row.RowIndex; //gets the row index selected

        TextBox tb = (TextBox)GridView1.Rows[index].FindControl("TextBox2");
        string YourRequiredText = tb.Text;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You must be doing something wrong. copy your latest code here. I just tested mine before uploading

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.