This is a migrated thread and some comments may be shown as answers.

Insert Items to radgrid with external textbox and button click

5 Answers 274 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ALEX
Top achievements
Rank 1
ALEX asked on 27 Nov 2012, 09:33 AM
Hi ,
 I need to perform radgrid insert operation with external textbox and button.I have one rad grid with data from sql server.I Placed textbox and button completely not related with radgrid.I need to insert values from external textbox to radgrid with external button click.

Can anyone pls help me on this

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 27 Nov 2012, 09:49 AM
Hi,

Please take a look into the following code snippet I tried to insert new item in the RadGrid on an external button click without opening the insert form of RadGrid.

C#:
protected void Button1_Click(object sender, EventArgs e)
{
    string OrderValue = TextBox1.Text;
    SqlConnection.Open();
    string insertQuery = "insert into Orders (OrderID) values ('" + OrderValue + "')"; //query to insert TextBox text in Database
    SqlCommand.CommandText = insertQuery;
    SqlCommand.Connection = SqlConnection;
    SqlCommand.ExecuteNonQuery();
    SqlConnection.Close();
    RadGrid1.Rebind();
}

Thanks,
Shinu.
0
ALEX
Top achievements
Rank 1
answered on 27 Nov 2012, 01:29 PM
I tried this ,only textbox values inserted in sql db ,the values not binded and shown in radgrid.
0
Shinu
Top achievements
Rank 2
answered on 28 Nov 2012, 06:37 AM
Hi Vignesh,

Please make sure that you are Rebinding the RadGrid in the Button Click event and the inserted DataField is  present in the RadGrid. After rebind, the newly inserted data also will be added in the RadGrid from the database. Check the entire code snippet I tried.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="OrderID" HeaderText="OrderID" SortExpression="OrderID"></telerik:GridBoundColumn>
        </columns>
    <MasterTableView>
</telerik:RadGrid>
 
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="button1_Click" />

C#:
protected void Button1_Click(object sender, EventArgs e)
{
    string OrderValue = TextBox1.Text;
    SqlConnection.Open();
    string insertQuery = "insert into Orders (OrderID) values ('" + OrderValue + "')"; //query to insert TextBox text in Database
    SqlCommand.CommandText = insertQuery;
    SqlCommand.Connection = SqlConnection;
    SqlCommand.ExecuteNonQuery();
    SqlConnection.Close();
    RadGrid1.Rebind();// Rebind the RadGrid
}

Thanks,
Shinu.
0
ALEX
Top achievements
Rank 1
answered on 28 Nov 2012, 10:06 AM
how for update and delete and also radgrid load data in pageload
0
Kostadin
Telerik team
answered on 03 Dec 2012, 10:43 AM
Hello Peter,

You could use the same approach as the one suggested by Shinu, just need to pass a different query string. For example if you would like to delete an item you have to pass the following query string:
string deleteQuery = "DELETE FROM [Products] WHERE [ProductID] = ('" + ProductID+ "')"; //query to item from Database
    SqlCommand.CommandText = deleteQuery ;


Greetings,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
ALEX
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
ALEX
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or