indikask

hi all

can any on explains how can i bind a collection to a data grid while the collection is filling. i'm using C# .Net compact frame work. my aim is reduse the time taken to the load item to the data grid. in that case i'm using a separate thread for fill the collection.the problem is datagrid only loads the items that are currently available in the collection not the all. i got more than 7,000 items.

how can i handel situation like this

thank you

indika




Re: Windows Forms Data Controls and Databinding binding custom collection to a data grid while the collection is filling..

Ken Tucker

Try something like this.

Code Block

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace DeviceApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
BindingList<BindTo> bl = new BindingList<BindTo>();

private void Form1_Load(object sender, EventArgs e)
{
DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = "BindingList`1";

DataGridTextBoxColumn colName = new DataGridTextBoxColumn();
colName.MappingName = "Name";
colName.HeaderText = "Name";
colName.Width = 100;

DataGridTextBoxColumn colId = new DataGridTextBoxColumn();
colId.MappingName = "ID";
colId.HeaderText = "ID";
colId.Width = 50;

ts.GridColumnStyles.Add(colName);
ts.GridColumnStyles.Add(colId);

dataGrid1.TableStyles.Add(ts);
dataGrid1.DataSource = bl;

Thread trd = new Thread(FillGrid);
trd.Start();
}

private void FillGrid()
{
for (int x = 0; x < 7000; x++)
{
Object[] obj = new object[] {new BindTo(x, "Item " + x.ToString())};
AddListItem d = new AddListItem(AddItem);
dataGrid1.Invoke(d , obj);
}
}

public delegate void AddListItem(BindTo bt);

private void AddItem(BindTo bt)
{
bl.Add(bt);
}
}

public class BindTo
{
private int mId;

public int ID
{
get { return mId; }
set { mId = value; }
}

private string mName;

public string Name
{
get { return mName; }
set { mName = value; }
}

public BindTo(int id, string name)
{
mId = id;
mName = name;
}


}
}






Re: Windows Forms Data Controls and Databinding binding custom collection to a data grid while the collection is filling..

indikask

thanks for your reply

its not working...(nothing displaied on grid view) still i'm in a truble...






Re: Windows Forms Data Controls and Databinding binding custom collection to a data grid while the collection is filling..

Gavin Jin - MSFT

Hi, if you want to bind a collection to a datagridview . There is a simple sample in MSDN

http://msdn2.microsoft.com/en-us/library/y0wfd4yz.aspx

You can try following code.

using System;

using System.Windows.Forms;

Public Enum Title

{

King,

Sir

};

public class EnumsAndComboBox : Form

{

private DataGridView dataGridView1 = new DataGridView();

private BindingSource bindingSource1 = new BindingSource();

Public EnumsAndComboBox()

{

this.Load += new System.EventHandler(EnumsAndComboBox_Load);

}

private void EnumsAndComboBox_Load(object sender, System.EventArgs e)

{

// Populate the data source.

bindingSource1.Add(new Knight(Title.King, "Uther", true));

bindingSource1.Add(new Knight(Title.King, "Arthur", true));

bindingSource1.Add(new Knight(Title.Sir, "Mordred", false));

bindingSource1.Add(new Knight(Title.Sir, "Gawain", true));

bindingSource1.Add(new Knight(Title.Sir, "Galahad", true));

// Initialize the DataGridView.

dataGridView1.AutoGenerateColumns = false;

dataGridView1.AutoSize = true;

dataGridView1.DataSource = bindingSource1;

dataGridView1.Columns.Add(CreateComboBoxWithEnums());

// Initialize and add a text box column.

DataGridViewColumn column = new DataGridViewTextBoxColumn();

column.DataPropertyName = "Name";

column.Name = "Knight";

dataGridView1.Columns.Add(column);

// Initialize and add a check box column.

column = new DataGridViewCheckBoxColumn();

column.DataPropertyName = "GoodGuy";

column.Name = "Good";

dataGridView1.Columns.Add(column);

// Initialize the form.

this.Controls.Add(dataGridView1);

this.AutoSize = true;

this.Text = "DataGridView object binding demo";

}

DataGridViewComboBoxColumn CreateComboBoxWithEnums()

{

DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();

combo.DataSource = Enum.GetValues(typeof(Title));

combo.DataPropertyName = "Title";

combo.Name = "Title";

return combo;

}

You can also check this thread for more information.

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=462605&SiteID=1

I am not sure it faster than your method, you can try it first.

If you have any further questions, feel free to let me know.

Best Regards

Gavin






Re: Windows Forms Data Controls and Databinding binding custom collection to a data grid while the collection is filling..

indikask

it is better you may see my quection again.. actualy i wants hide the delay from the user when the items i loding to the collection..