C#DataGridView BindingList

时间:2016-10-11 23:25:59

标签: 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)
    {

    }


}

2 个答案:

答案 0 :(得分:1)

您需要将DataBoundItem转换为IInstance而不是DataRowView

&#39; as&#39;如果类型转换失败,opeartor将返回null。将它直接转换为您期望的类型会更安全,这样如果您犯了错误,您的代码就会失败。

答案 1 :(得分:0)

我没有在脚本中注意到数据源。你能试试吗?

SQL Server:

Page

MS Access:

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());
            }
        }
    }
}