PowerPacks.DataRepeater with DataGridView

  • Thread starter Thread starter Tomasz Jastrzebski
  • Start date Start date
T

Tomasz Jastrzebski

Hi,

I want to display all DataSet tables in one DataRepeater. On the Form I have
DataRepeater with one DataGridView.
Then I try to programmatically create new DataRepeater items and bind my
DataGridView controls to DataTables.
The code fails with NullReferenceException at
Microsoft.VisualBasic.PowerPacks.NullDataManager.AddNew()
I spent entire afternoon looking for some useful examples and found nothing.

Could anyone help?

Thomas

sample test code:

private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++) {
DataTable table = new DataTable();

table.Columns.Add("test1");
table.Columns.Add("test2");

this.dataRepeater1.AddNew(); // causes NullReferenceException
// at Microsoft.VisualBasic.PowerPacks.NullDataManager.AddNew()
DataGridView grid =
(DataGridView)this.dataRepeater1.CurrentItem.Controls[0];
grid.DataSource = table;
}
}
 
Hi Tomasz,

Welcome to Microsoft Managed Newsgroup, I'm Zhi-Xin Ye, it's my pleasure to
work with you on this issue.

To display all the DataSet tables in a DataRepeater, we can store all the
tables in a List<DataTable> object, then bind this List<DataTable> object
to the DataReapter, meanwhile handle the DataReapter.DrawItem event to
specify corresponding data source for each DataGridView.

The following code demonstrates how to display mutilple datatables in
DataRepeater controls.

private void Form1_Load(object sender, EventArgs e)
{
//Create an item template
DataGridView dataGridView1 = new DataGridView();
dataGridView1.Name = "dataGridView1";
dataGridView1.Width = this.dataRepeater1.Width;
this.dataRepeater1.ItemTemplate.Controls.Add(dataGridView1);

for (int j = 1; j < 5; j++)
{
DataTable dt = new DataTable("dt"+j.ToString());
for (int n = 0; n < j; n++)
{
dt.Columns.Add("c" + n.ToString());
}
for (int k = 0; k < 5; k++)
{
dt.Rows.Add(k.ToString());
}
tables.Add(dt);
}

this.dataRepeater1.DrawItem +=
new
Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventHandler(dataRepeater1_
DrawItem);
this.dataRepeater1.DataSource = tables;
}

List<DataTable> tables = new List<DataTable>();

void dataRepeater1_DrawItem(object sender,
Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs e)
{
DataTable dt = this.tables[e.DataRepeaterItem.ItemIndex];
DataGridView dgv =
(DataGridView)e.DataRepeaterItem.Controls["dataGridView1"];
dgv.DataSource = dt;
}

Please try my suggestions and let me know whether it makes sense to you. If
you have any questions or concerns, please feel free to let me know.

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks much! It works. I just made one modification - I bound DataRepeater
to DataSet.Tables collection insted.
Thomas
 
Back
Top