[英]C# DataGridView throws exception when removing object from BindingList
[英]C# DataGridView BindingList
有人可以幫助我解釋一下為什么我在以下代碼中為DataBoundItem獲取空值的原因:
public partial class ucInstanceSearch : UserControl
{
private IStorage tempStorage;
private BindingList<IInstance> instanceData;
public ucInstanceSearch(IStorage new_Storage)
{
InitializeComponent();
this.tempStorage = new_Storage;
instanceData = new BindingList<IInstance>(tempStorage.Instance);
InitalizeInstanceTable();
}
private void InitalizeInstanceTable()
{
dgInstanceTable.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgInstanceTable.MultiSelect = false;
dgInstanceTable.AutoGenerateColumns = false;
dgInstanceTable.RowHeadersVisible = false;
dgInstanceTable.DataSource = instanceData;
}
private void PopulateInstanceTable(String searchFilter)
{
dgInstanceTable.Update();
}
private void dgInstanceTable_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void btnSearch_Click(object sender, EventArgs e)
{
if (txtSearch.Text != "")
{
PopulateInstanceTable(txtSearch.Text);
}
else
{
MessageBox.Show("Enter Data");
}
}
private void btnSelect_Click(object sender, EventArgs e)
{
MessageBox.Show(dgInstanceTable.SelectedRows[0].Cells[2].Value + string.Empty);
DataRow row = (dgInstanceTable.SelectedRows[0].DataBoundItem as DataRowView).Row;
IInstance selected = (IInstance)row;
textBox1.Text = selected.URL;
}
private void ucInstanceSearch_Load(object sender, EventArgs e)
{
}
}
您需要將DataBoundItem
IInstance
轉換為IInstance
類型, IInstance
不是DataRowView
類型。
如果類型轉換失敗,則“ as”操作者將返回null
。 將其直接轉換為您期望的類型是更安全的,這樣,如果您犯了錯誤,代碼將失敗。
我沒有注意到您的腳本中有數據源。 你可以試試這個嗎?
SQL Server:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
string connetionString;
SqlConnection connection;
SqlDataAdapter adapter;
SqlCommandBuilder cmdBuilder;
DataSet ds = new DataSet();
DataSet changes;
string Sql;
Int32 i;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
connection = new SqlConnection(connetionString);
Sql = "select * from Product";
try
{
connection.Open();
adapter = new SqlDataAdapter(Sql, connection);
adapter.Fill(ds);
connection.Close();
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
cmdBuilder = new SqlCommandBuilder(adapter);
changes = ds.GetChanges();
if (changes != null)
{
adapter.Update(changes);
}
MessageBox.Show("Changes Done");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
MS Access:
using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
string connetionString;
OleDbConnection connection;
OleDbDataAdapter oledbAdapter;
OleDbCommandBuilder oledbCmdBuilder;
DataSet ds = new DataSet();
DataSet changes;
int i;
string Sql;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;";
connection = new OleDbConnection(connetionString);
Sql = "select * from tblUsers";
try
{
connection.Open();
oledbAdapter = new OleDbDataAdapter(Sql, connection);
oledbAdapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
oledbCmdBuilder = new OleDbCommandBuilder(oledbAdapter);
changes = ds.GetChanges();
if (changes != null)
{
oledbAdapter.Update(ds.Tables[0]);
}
ds.AcceptChanges();
MessageBox.Show("Save changes");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.