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

Add new row to the radgrid

1 Answer 97 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Smitha
Top achievements
Rank 1
Smitha asked on 05 Mar 2014, 10:47 PM
Hi,
  I want to add data to a grid which is already binded to a data source. I want to have the  user copy and paste data from excel to a text box, and has a button which when clocked should add the data to the datagrid. I am looking to add only one column. The grid has multiple columns, but is is only one column of data that needs to be added as new rows to the grid.
Thanks,

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 06 Mar 2014, 10:30 AM
Hi Smitha,

Please try the following code snippet to achieve your scenario.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1">
</telerik:RadGrid>
<asp:Label ID="Label1" runat="server" Text="Country Name">
</asp:Label>
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Add Row" OnClick="Button1_Click" />

C#:
protected void Button1_Click(object sender, EventArgs e)
{
    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Northwind_newConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(connectionString);
    conn.Open();
    string cmdText = "INSERT INTO Countries (Name) VALUES(@Name)";
    SqlCommand cmd = new SqlCommand(cmdText, conn);
    cmd.Parameters.AddWithValue("@Name", TextBox1.Text);
    cmd.ExecuteNonQuery();
    conn.Close();
    RadGrid1.DataBind();
}

Thanks,
Princy.
Tags
Grid
Asked by
Smitha
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or