开发者

How do I add row data directly to a DataGrid Class?

开发者 https://www.devze.com 2023-04-10 06:19 出处:网络
How do I add row data directly to a DataGrid Class? I am using a free opensource class from a company that I will not name (even if it is RadiantQ) that I like a lot.It has this cool MuLtiColumnTreeL

How do I add row data directly to a DataGrid Class?

I am using a free opensource class from a company that I will not name (even if it is RadiantQ) that I like a lot. It has this cool MuLtiColumnTreeList control that is a combination of a tree control and a datagrid. It comes with an example code that you can look at and everything. It is pretty cool. It is inherited from the DataGrid class.

The problem is that I am kind of new to databinding on this level and so I would like to just go ahead and write some code that forces the data that I have gathered from another class into the control.

So I looked online for how to do that for a DataGrid class and the information is not easily available. Can anyone help?

It seems that once the databinding is done, and if you change the data you have to rebind to the control. This is what was giving me difficulty before. So what I have to do is run some command like this:

this.mutlicoolgridview.ItemsSource = null; this.mutlicoolgridview.ItemsSource = SampleData.GetSampleDataNew();

The problem I am having now is this. After running his command about one th开发者_StackOverflowousand times, I actually run out of memory. I think that doing this:

this.mutlicoolgridview.ItemsSource = null;

is not such a good idea. Is there a better command to do to free up the memory?

This is a similar looking crash: [

How do I add row data directly to a DataGrid Class?

]


If you have a List of objects, you can copy them to a BindingList. Then you can use

dataGrid.ItemsSource = myBindingList;


To add rows to a DataGrid, you will need to, first, bind a DataSource to the DataGrid and, then, add rows to your DataSource.

Valid DataSources are:

  • A DataTable
  • A DataView
  • A DataSet
  • A DataViewManager
  • A single dimension array
  • Any component that implements the IListSource interface
  • Any component that implements the IList interface

Here's an Windows Form example of adding a row to a DataTable that is bound to the DataGrid:

public partial class Form1 : Form
{
    // Instantiate the DataSource that will be bound to the DataGrid
    DataSet dataSet = new DataSet("MyDataSet");
    DataTable dataTable = new DataTable("MyDataTable");

    public Form1()
    {
        InitializeComponent();

        this.dataSet.Tables.Add(this.dataTable);
        this.dataTable.Columns.Add(new DataColumn("Date"));

        // Bind the DataTable to the DataGrid
        this.dataGrid1.SetDataBinding(this.dataSet, "MyDataTable");
    }    

    private void button1_Click(object sender, EventArgs e)
    {
        // When the user clicks the button, add a new row to the DataTable
        DataRow dr = this.dataTable.NewRow();
        dr["Date"] = DateTime.Now;
        this.dataTable.Rows.Add(dr);
    }
}

I recommend that you create a throw away project and play around with the DataGrid class to get familiar with the different ways that the DataGrid works with DataSources.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号