在 RadGridView 中编辑数据
Editing data in RadGridView
我正在尝试编辑 RadGridView 中的单元格,该单元格的数据源集来自 SQL 视图的数据。
我希望做的是从视图中填充网格,允许编辑,然后在用户单击 "Update" 按钮时手动更新关联的 table。
一旦用户将数据输入单元格并离开,单元格就会出现错误 "Specified method is not supported"。
我假设它正在尝试使用新值更新数据源,所以我正在尝试找出如何告诉它不要这样做。
我用以下内容填充 table:
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;
}
根据 RadGrid data binding documentation,DataSource
属性 接受以下类型的实例:
DataSet
DataTable
DataView
- Array of
DataRow
- Any object collection that implements these interfaces:
IListSource
IList
IEnumerable
ICustomTypeDescriptor
根据参考检查,SqlDataReader
不受支持,因为它没有实现上述接口,因此 NotSupportedException
在将 SqlDataReader
内容绑定到 [=12 时抛出=] 属性直接。
作为解决方法,您可以创建新的 DataTable
实例并从 SqlDataReader
中填充其内容,如下所示:
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
注:
构建 SQL 查询的字符串连接可能容易 SQL 注入。将服务器控制值传递给 SQL 查询时使用参数是更推荐的方式:
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;
}
相关问题:
Populate data table from data reader
我正在尝试编辑 RadGridView 中的单元格,该单元格的数据源集来自 SQL 视图的数据。
我希望做的是从视图中填充网格,允许编辑,然后在用户单击 "Update" 按钮时手动更新关联的 table。
一旦用户将数据输入单元格并离开,单元格就会出现错误 "Specified method is not supported"。
我假设它正在尝试使用新值更新数据源,所以我正在尝试找出如何告诉它不要这样做。
我用以下内容填充 table:
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;
}
根据 RadGrid data binding documentation,DataSource
属性 接受以下类型的实例:
DataSet
DataTable
DataView
- Array of
DataRow
- Any object collection that implements these interfaces:
IListSource
IList
IEnumerable
ICustomTypeDescriptor
根据参考检查,SqlDataReader
不受支持,因为它没有实现上述接口,因此 NotSupportedException
在将 SqlDataReader
内容绑定到 [=12 时抛出=] 属性直接。
作为解决方法,您可以创建新的 DataTable
实例并从 SqlDataReader
中填充其内容,如下所示:
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
注:
构建 SQL 查询的字符串连接可能容易 SQL 注入。将服务器控制值传递给 SQL 查询时使用参数是更推荐的方式:
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;
}
相关问题:
Populate data table from data reader