【问题标题】:C# DataGridView BindingListC# DataGridView 绑定列表
【发布时间】:2016-10-11 23:25:58
【问题描述】:

有人可以帮我解释一下为什么我在以下代码中得到了 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)
    {

    }


}

【问题讨论】:

    标签: c# datagridview bindinglist


    【解决方案1】:

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

    如果类型转换失败,'as' 运算符将返回 null。将其直接转换为您期望的类型会更安全,这样如果您犯了错误,您的代码就会失败。

    【讨论】:

      【解决方案2】:

      我没有注意到您的脚本中有数据源。你可以试试这个吗?

      SQL 服务器:

      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 访问权限:

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

      【讨论】:

        猜你喜欢
        • 2012-09-14
        • 1970-01-01
        • 1970-01-01
        • 2018-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-29
        • 1970-01-01
        相关资源
        最近更新 更多