datatagridview有行,但datatsourse显示为空?

时间:2017-06-29 09:29:08

标签: c# winforms datagridview

我在UI中手动添加了datagridview中的行。 现在我希望数据源填充数据表,但数据是空的。

enter image description here

3 个答案:

答案 0 :(得分:1)

由于您已经手动向GridView添加了行,并且您没有设置DataSource,所以显然数据源将为空或为空,

你只需要迭代所有行并将它们绑定到datatable。

                DataTable dt = new DataTable();
                dt.Columns.Add("Date", typeof(DateTime));
                dt.Columns.Add("Particular", typeof(string));
                dt.Columns.Add("Debit", typeof(int));
                dt.Columns.Add("Credit", typeof(int));
                dt.Columns.Add("Balance", typeof(string));
                dt.Columns.Add("Summary", typeof(string));


        private void bindGridtoDataTable()
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
                dt.Rows.Add(row.Cells["Date"].Value, row.Cells["Particular"].Value, row.Cells["Debit"].Value, row.Cells["Credit"].Value, row.Cells["Balance"].Value, row.Cells["Summary"].Value);
        }

答案 1 :(得分:0)

以下是一个很好的示例:Save datagridView into sql database

您需要区分是否已删除,修改或添加行。

var dataTable = ((DataTable)dataGridView1.DataSource).GetChanges();
if(dataTable != null && dataTable.Rows.Count > 0)
{
  foreach (DataRow row in dataTable.Rows)
        {
            switch (row.RowState)
            {
                case DataRowState.Added:
                    // DO INSERT QUERY
                    break;
                case DataRowState.Deleted:
                    // DO DELETE QUERY
                    break;
                case DataRowState.Modified:
                    SqlCommand command = new SqlCommand("UPDATE YOURTABLE SET TypNastaveniaID = @typ, Nazov = @title, Hodnota = @amount");
                    command.Parameters.Add(new SqlParameter("@typ", row["TypNastaveniaID"]));
                    command.Parameters.Add(new SqlParameter("@title", row["Nazov"]));
                    command.Parameters.Add(new SqlParameter("@amount", row["Hodnota"]));
                    command.ExecuteNonQuery();
                    break;
            }
        }
    ((DataTable)dataGridView1.DataSource).AcceptChanges();
}

答案 2 :(得分:0)

这里的一些答案建议你摆弄DataGridView的行。 我强烈建议你不要这样做,但要使用BindingSource和BindingList!

我认为问题在于您将收藏品直接分配到数据源,而不是通过BindingList

如果您有课程,请说明MyClass您要在DataGridView中显示序列,并希望通过添加/添加/删除此序列中的项目删除/更改DataGridView中的行应该执行以下操作

  • 将您的商品设为“IEnumerable
  • 将它们包装在BindingList中
  • 将此内容分配给bindingSource1.DataSource
  • 将bindingSource分配给dataGridView1.DataSource

大部分内容都可以在visual studio designer中完成。您唯一要做的就是将数据包装在BindingList中,并将BindingList分配给BindingSource的DataSource:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponents();
    }

    private void InitialeDataGridView
    {
        IEnumerable<MyClass> dataToShow = ...
        this.BindingSource1.DataSource = new BindingList<MyClass>(dataToShow.ToList());
    }

    private void Form1_Load(object sender, ...)
    {
        this.InitializeDataGridView();
    }

    private void button1_click(object sender, ...)
    {
        IEnumerable<MyClass> editedData = (BindingList<MyClass>)this.bindingSource1.DataSource;
        ProcessEditedData(editedData);
    }
}

您将看到所有已编辑的数据。但是,您的数据不可排序。为此,您可以创建一个SortableBindingList,在Stackoverflow上描述。但是,安装Nuget BindingListView

要容易得多

这会稍微改变您的代码,但可以启用排序和过滤:

您使用BindingListView<MyClass>而不是BindingList。您的代码只会略有变化:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponents();
        this.myItems= new BindingListView<MyClass>(this.components);
        this.bindingSource1.DataSource = this.myItems;
    }

    private readonly BindingListView<MyClass> myItems;

    private void InitialeDataGridView
    {
        IEnumerable<MyClass> dataToShow = ...
        this.myItems = dataToShow.ToList();
    }

    private void Form1_Load(object sender, ...)
    {
        this.InitializeDataGridView();
    }

    private void button1_click(object sender, ...)
    {
        IEnumerable<MyClass> editedData = (BindingListView<MyClass>)this.bindingSource1.DataSource;
        ProcessEditedData(editedData);
    }
}

然后,您可以通过单击列标题对列进行排序。看到自动更改排序列标题中的排序字形,您将获得额外奖励。

过滤

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if (this.checkBox1.Checked)
    {   // show only items with Id == 0
        Predicate<MyClass> pr = p => p.Id == 0;
        this.persons.ApplyFilter(pr);
    }
    else
       this.persons.RemoveFilter();
}

就是这么简单:添加/删除/更改/排序/过滤:只需几行代码即可。