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

Grid without autogenerated columns example

3 Answers 71 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kirie
Top achievements
Rank 1
Kirie asked on 15 Sep 2011, 08:12 AM
Hi,

I found a example in the documentation, but I did not find a full example with edit row / delete row and so on.

Do you have code example with those features?

Using the example available i was able to doble click a row to edit, but I am having trouble saving the row (index out of bounds).

Edit: Forgot to meintion that i need a example where the structure and everything else is in the code behind file, due to dynamic number of columns.

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 15 Sep 2011, 11:20 AM
Hello Kirie,

Try the following code snippet using NeedDataSource to Edit,Insert and Delete operations.
C#:
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
 GridEditFormItem UpdateItem = (GridEditFormItem)e.Item;
 TextBox txtname = (TextBox)UpdateItem["EmployeeName"].Controls[0];
 string name = txtname.Text;
 TextBox txtid = (TextBox)UpdateItem.FindControl("txtid");
 TextBox txtlastname = (TextBox)UpdateItem["LastName"].Controls[0];
 string lastname = txtlastname.Text;
 SqlCommand cmd1 = new SqlCommand("update Employees set FirstName=@FirstName,LastName=@LastName where EmployeeId=@EmployeeId", con);
 int empId = Convert.ToInt32(UpdateItem.GetDataKeyValue("EmployeeID").ToString());
 cmd1.Parameters.Add(new SqlParameter("@FirstName", name));
 cmd1.Parameters.Add(new SqlParameter("@LastName", lastname));
 cmd1.Parameters.Add(new SqlParameter("@EmployeeID", empId));
 con.Open();
 cmd1.ExecuteNonQuery();
 con.Close();      
}
 
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
 GridEditFormInsertItem InsertItem = (GridEditFormInsertItem)e.Item;
 TextBox txtname = (TextBox)InsertItem["EmployeeName"].Controls[0];
 string name = txtname.Text;
 TextBox txtlastname = (TextBox)InsertItem["LastName"].Controls[0];
 string lastname = txtlastname.Text;
 SqlCommand cmd1 = new SqlCommand("insert into Employees(FirstName,LastName) values(@FirstName,@LastName)", con);
 cmd1.Parameters.Add(new SqlParameter("@FirstName", name));
 cmd1.Parameters.Add(new SqlParameter("@LastName", lastname));
 con.Open();
 cmd1.ExecuteNonQuery();
 con.Close();
}

protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
 {
  GridEditFormInsertItem DeleteItem= (GridEditFormInsertItem)e.Item;
  TextBox txtname = (TextBox)DeleteItem["EmployeeName"].Controls[0];
  string name = txtname.Text;
  TextBox txtlastname = (TextBox)DeleteItem["LastName"].Controls[0];
  string lastname = txtlastname.Text;
  SqlCommand cmd1 = new SqlCommand("delete from Employees(FirstName,LastName) values(@FirstName,@LastName)", con);
  cmd1.Parameters.Add(new SqlParameter("@FirstName", name));
  cmd1.Parameters.Add(new SqlParameter("@LastName", lastname));
  con.Open();
  cmd1.ExecuteNonQuery();
  con.Close();
}

Thanks,
Princy.
0
Kirie
Top achievements
Rank 1
answered on 15 Sep 2011, 11:34 AM
Thanks for your swift reply Princy,

but I am looking for an example with all the code, except from from what you posted. My problem is the following:

I have created an dynamic grid in the code behding, and on the NeedDataSource event i create the rows and inserts the data.

I can doubleclick a column to edit, but when I try to edit an other row by clicking it, or save the current one i get a index out of bounds exception. Seems like the data is no longer present.

Therefor i need an example that shows when to add data to the grid and so on, when the grid is structured in the codebehind, but added without rows / columns in the markup.
0
Iana Tsolova
Telerik team
answered on 20 Sep 2011, 02:44 PM
Hello Kirie,

A possible reason for the issue is if you are not creating the grid structure properly. Can you confirm you follow the steps and requirements described here?

Regards,
Iana Tsolova
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
Kirie
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Kirie
Top achievements
Rank 1
Iana Tsolova
Telerik team
Share this question
or