I'm creating my first Visual Web Part project with Visual Studio 2010 (C#). The purpose of this simple project is to become better aquainted with the RadGrid control. There are basically two parts to it:
1. Add RadGrid control and bind to datasource using C#. The RadGrid has an edit and delete column.
2. Add a RadTextBox control to display the captured content of a specific cell when I click on the update option when editing the record.
Part 1 is pretty straightforward and seems to work fine. Here is the code that I run upon page load:
private
void
bindGrid(RadGrid grid)
{
string
qry =
"SELECT Recipient, Email FROM Table1"
;
SqlConnection conn =
new
SqlConnection(connection
string
);
SqlCommand cmd =
new
SqlCommand(qry, conn);
SqlDataAdapter adapter =
new
SqlDataAdapter(cmd);
DataTable recipients =
new
DataTable();
try
{
conn.Open();
adapter.Fill(recipients);
}
catch
(Exception ex)
{
}
finally
{
grid.DataSource = recipients;
grid.DataBind();
conn.Close();
}
}
The problem is with part 2. When I click on edit then update I want to capture the value in that row for the column labeled "Recipient".
I try to do this with the following code:
protected
void
Update_Command(
object
sender, GridCommandEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem dataItem = e.Item
as
GridDataItem;
RadTextBox1.Text = dataItem[
"recipient"
].Text.ToString();
}
else
if
(e.Item
is
GridEditFormItem)
{
}
}
Instead of getting the persons name displayed in the cell, all I get is " "; I've tried this several different ways, even using
the NeedDataSource method. The result is always the same.
Out of curiousity I counted the columns in the RadGrid immediately after binding with RadGrid1.columns.count and get 0. So the RadGrid displays the data from the datasource properly on page load, but I can't seem to access any of that data.
I can't figure this out and any guidance would be greatly appreciated.