Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Accessing DataTable rows at design time
 

Accessing DataTable rows at design time

Hello,

At design time in VS2003 I can access the rows of a DataTable which has been connected to via a DataAdapter. I've got a very simple UserControl as an example to show you how I do it:

    [Designer(typeof(UserControl1.UserControl1Designer))]
    public class UserControl1 : System.Windows.Forms.UserControl
    {
         //standard UserControl class with no changes
      
         //nested Designer class
         internal class UserControl1Designer : System.Windows.Forms.Design.ControlDesigner
        {
            public UserControl1Designer() : base()
            {
                AddVerbs();
            }

            private void AddVerbs()
            {
                Verbs.Add(new DesignerVerb("Test",new EventHandler(OnTest)));
            }

            private void OnTest(object sender, EventArgs e)
            {
                System.Windows.Forms.Form f = base.Control.FindForm();
                IContainer co = f.Container;

                MessageBox.Show("OnTest");

                if (co!=null)
                {
                    foreach (object o in co.Components)
                    {
                        if (o is DataAdapter)
                        {
                            DataAdapter d=o as DataAdapter;
                            DataSet dataset=new DataSet();
                            d.Fill(dataset);
                            DataTable table = dataset.Tables[0];
                            MessageBox.Show(table.Rows.Count.ToString());
                        }
                    }
                }
            }
        }
    }

However, at design time in VS2005 the automatically generated TableAdapter base class is a Component with no Fill method, meaning that I can't access the underlying DataTable's rows. It really would be most useful if the automatically generated TableAdapters implemented the IDataAdapter interface to allow something similar to the above to work. In the absence of such an implemention, does anybody know how I can access DataTable rows at design time in VS2005 using my UserControl?

Many thanks!
Christopher Ireland  Wednesday, January 11, 2006 2:49 PM
Well, one way of doing it, if you know the name of the DataTable, would be the following:

            private void OnTest(object sender, EventArgs e)
            {
                System.Windows.Forms.Form f = base.Control.FindForm();
                IContainer co = f.Container;
                string tableName = "Employee";
                string adapter = "TableAdapter";
                string dataSet = "DataSet";

                MessageBox.Show("OnTest");

                object dataTable=null;
 
                if (co!=null)
                {
                    foreach (object o in co.Components)
                    {
                        if (o is DataAdapter)
                        {
                            DataAdapter d = o as DataAdapter;
                            DataSet dataset = new DataSet();
                            d.Fill(dataset);
                            DataTable table = dataset.Tables[0];
                            MessageBox.Show(table.Rows.Count.ToString());
                        }

                        if (o.GetType().Name.Contains(dataSet))
                        {
                            Type TdataSet = o.GetType();
                            PropertyInfo name = TdataSet.GetProperty(tableName);
                            dataTable = name.GetValue(o, null);
                        }
                    }

                    foreach (object o in co.Components)
                    {
                        if (o.GetType().Name.Contains(adapter) && dataTable != null)
                        {
                            Type tableAdapter = o.GetType();
                            MethodInfo fill = tableAdapter.GetMethod("Fill");
                            fill.Invoke(o, new object[] { dataTable });
                            MessageBox.Show((dataTable as DataTable).Rows.Count.ToString());
                        }
                    }
                }
            }

Having TableAdapters implement the IDataAdapter interface would be much simpler IMO!
Christopher Ireland  Wednesday, January 11, 2006 4:44 PM

You can use google to search for other answers

Custom Search

More Threads

• Individual Cell Change of Color
• Does DataGridView support active sort?
• in vb.net programming i have a requriment of a control like the message we type while posting a thread in forums
• DataGridViewLinkColumn & DataBinding
• serious problem with BindingManagerBase and its definitly a bug
• Spreadsheet in C#
• How to set DataGridViewComboBoxColumn to display nothing ?
• Format for CONTAINS parameter in SELECT command.
• Item cannot be added to a read-only or fixed-size list
• Help with periodically refreshing data in my dataGridView, threaded