ErrorProvider.DataSource 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置 ErrorProvider 监视的数据源。
public:
property System::Object ^ DataSource { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public object DataSource { get; set; }
public object DataSource { get; set; }
[<System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.DataSource : obj with get, set
member this.DataSource : obj with get, set
Public Property DataSource As Object
属性值
要被监视以查找错误的基于 IList 接口的数据源。 通常情况下,这是一个要被监视错误的 DataSet。
- 属性
示例
下面的代码示例演示如何使用 ErrorProvider a DataSource 和 DataMember 指示用户的数据错误。 此代码假定你已创建并填充了包含DataSet命名的命名dataSet1``dataTable1
名称DataTable。 将控件绑定到 DataSet 控件(如 DataGridView 控件)时,通过 DataRow 对象指定的错误会显示为控件中的 DataGridView 错误标志符号。
private:
void InitializeComponent()
{
// Standard control setup.
//....
// You set the DataSource to a data set, and the DataMember to a table.
errorProvider1->DataSource = dataSet1;
errorProvider1->DataMember = dataTable1->TableName;
errorProvider1->ContainerControl = this;
errorProvider1->BlinkRate = 200;
//...
// Since the ErrorProvider control does not have a visible component,
// it does not need to be added to the form.
}
private:
void buttonSave_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
// Checks for a bad post code.
DataTable^ CustomersTable;
CustomersTable = dataSet1->Tables[ "Customers" ];
System::Collections::IEnumerator^ myEnum = (CustomersTable->Rows)->GetEnumerator();
while ( myEnum->MoveNext() )
{
DataRow^ row = safe_cast<DataRow^>(myEnum->Current);
if ( Convert::ToBoolean( row[ "PostalCodeIsNull" ] ) )
{
row->RowError = "The Customer details contain errors";
row->SetColumnError( "PostalCode", "Postal Code required" );
}
}
}
private void InitializeComponent()
{
// Standard control setup.
//....
// You set the DataSource to a data set, and the DataMember to a table.
errorProvider1.DataSource = dataSet1 ;
errorProvider1.DataMember = dataTable1.TableName ;
errorProvider1.ContainerControl = this ;
errorProvider1.BlinkRate = 200 ;
//...
// Since the ErrorProvider control does not have a visible component,
// it does not need to be added to the form.
}
private void buttonSave_Click(object sender, System.EventArgs e)
{
// Checks for a bad post code.
DataTable CustomersTable;
CustomersTable = dataSet1.Tables["Customers"];
foreach (DataRow row in (CustomersTable.Rows))
{
if (Convert.ToBoolean(row["PostalCodeIsNull"]))
{
row.RowError="The Customer details contain errors";
row.SetColumnError("PostalCode", "Postal Code required");
}
}
}
Private Sub InitializeComponent()
' Standard control setup.
'....
' You set the DataSource to a data set, and the DataMember to a table.
errorProvider1.DataSource = dataSet1
errorProvider1.DataMember = dataTable1.TableName
errorProvider1.ContainerControl = Me
errorProvider1.BlinkRate = 200
End Sub
'...
' Since the ErrorProvider control does not have a visible component,
' it does not need to be added to the form.
Private Sub buttonSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Checks for a bad post code.
Dim CustomersTable As DataTable
CustomersTable = dataSet1.Tables("Customers")
Dim row As DataRow
For Each row In CustomersTable.Rows
If Convert.ToBoolean(row("PostalCodeIsNull")) Then
row.RowError = "The Customer details contain errors"
row.SetColumnError("PostalCode", "Postal Code required")
End If
Next row
End Sub
注解
这是 DataSource 一个数据源,可以附加到控件,并且要监视错误。 DataSource 可以设置为实现 IList的任何集合。
为了避免在运行时发生更改时DataSource发生冲突,DataMember应使用BindToDataAndErrors而不是单独设置DataSourceDataMember。