0

I am trying to edit a cell in a RadGridView which has a datasource set from data from an SQL view.

What I was hoping to do was to populate the grid from the view, allow edits then manually update the associated tables when a user click an "Update" button.

As soon as the user enters data into a cell and leaves the cell is presents with an error "Specified method is not supported".

I assume it's trying to update the datasource with the new value so I am trying to work out how to tell it not to.

I am populating the table with:

using (SqlConnection con = new SqlConnection(mydatasource))
{
    con.Open();
    SqlCommand cmd = new SqlCommand("select SRID, Name, Result from EditBatchResultsView where SRID = " + drpSRID.Text, con);
    SqlDataReader reader = cmd.ExecuteReader();

    radGridView1.DataSource = reader;
}
Dean Harry
  • 277
  • 1
  • 3
  • 13
  • It is better to analyze if you could provide stack trace, probably it's related to `SqlDataReader` instance because `RadGrid`'s `DataSource` property may only accept `DataTable` or `IEnumerable` sources. – Tetsuya Yamamoto Feb 27 '19 at 04:06

1 Answers1

0

According to RadGrid data binding documentation, the DataSource property accepts instances of the following types:

  • DataSet
  • DataTable
  • DataView
  • Array of DataRow
  • Any object collection that implements these interfaces:
    • IListSource
    • IList
    • IEnumerable
    • ICustomTypeDescriptor

Based from reference inspection, SqlDataReader doesn't supported because it doesn't implement those interfaces mentioned above, hence NotSupportedException has thrown when binding SqlDataReader contents into DataSource property directly.

As a workaround, you may create new DataTable instance and fill its contents from SqlDataReader like this:

var dt = new DataTable();

using (SqlConnection con = new SqlConnection(mydatasource))
{
    con.Open();
    SqlCommand cmd = new SqlCommand("select SRID, Name, Result from EditBatchResultsView where SRID = " + drpSRID.Text, con);
    SqlDataReader reader = cmd.ExecuteReader();

    // fill DataTable contents
    dt.Load(reader);

    // assign DataTable as data source instead
    radGridView1.DataSource = dt;
}

// DataBind goes here

Note:

The string concatenation to build SQL query may prone to SQL injection. Using parameters when passing server control value to SQL query is more recommended way:

var dt = new DataTable();

using (SqlConnection con = new SqlConnection(mydatasource))
{
    con.Open();
    SqlCommand cmd = new SqlCommand("select SRID, Name, Result from EditBatchResultsView where SRID = @SRID", con);
    cmd.Parameters.Add("@SRID", SqlDbType.VarChar).Value = drpSRID.Text;
    SqlDataReader reader = cmd.ExecuteReader();

    // fill DataTable contents
    dt.Load(reader);

    // assign DataTable as data source instead
    radGridView1.DataSource = dt;
}

Related issue:

Populate data table from data reader

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61