2

i have a small program that takes a datatable (takes data from sql database) then splits it into datatable array by field and then should display it in a tabcontrol, each field in it's own tab

the split, takes single datatable and splits into datatable array, works fine i think

public DataTable[] splitTable(DataTable mainDT,string columnName)
    {
        int tmp=0;
        DataTable[] splitDT = new DataTable[11];
        for (int i=0;i<11;i++)
            splitDT[i]=new DataTable();



        foreach (DataRow row in mainDT.Rows)
        {              
           tmp = row[columnName].GetHashCode();
           splitDT[tmp].ImportRow(row);
        }
        return splitDT;
    }

here is the problem part

public Display(string Name, string rname, DataTable[] left,int tabNum)
    {
        InitializeComponent();
        TabPage tp;
        DataGridView dgw;

        lLeftTable.Text = Name;

        for (int i = 0; i < tabNum;i++ )
        {
            tp = new TabPage(""+i);
            dgw = new DataGridView();
            dgw.DataSource = left[i];
            tp.Controls.Add(dgw);
            tbcLeftPages.Controls.Add(tp);
            tbcLeftPages.Refresh();
        }

    }

it opens a tabcontrol with the right amount of tabs but no data in them

EDIT 1 still no good, show tabs with no gridview changed it into a function that get's parts of the datatable array

 public void addDGWtoTab(DataTable dt,string side,int num)
        {
        MessageBox.Show("table:" + side + " bucket:" + num + "rows:" + dt.Rows.Count);
        DataGridView dgw = new DataGridView();
        TabPage tp = new TabPage();

        //data grid view
        dgw.Name = "dgv" + num;
        dgw.AutoSize = true;
        dgw.Dock = DockStyle.Fill;

        //tab page
        tp.Name = "tp" + num;
        tp.Text = "Bucket " + num;
        tp.Tag = dt.Rows.Count;
        tp.TabIndex = num;

        if (side == "left")             
            tbcLeftPages.Controls.Add(tp);   
        else tbcRightPages.Controls.Add(tp);
        dgw.DataSource = dt; 
        tp.Controls.Add(dgw);

    }

EDIT 2 added spitDT

public DataTable[] splitTable(DataTable mainDT,string columnName,int mod)
    {
        DataTable[] splitDT = new DataTable[11];
        for (int i=0;i<11;i++)
            splitDT[i]=new DataTable();

        int splitINT;
        int tmp=0;

        foreach (DataRow row in mainDT.Rows)
        {              
           splitINT = row[columnName].GetHashCode();
           tmp = splitINT % mod;
           splitDT[tmp].ImportRow(row);
        }

        return splitDT;
    }

EDIT 3 split with messages

public DataTable[] splitTable(DataTable mainDT,string columnName,int mod)
    {
        DataTable[] splitDT = new DataTable[11];
        for (int i=0;i<11;i++)
            splitDT[i]=new DataTable();

        int splitINT;
        int tmp=0;

        foreach (DataRow row in mainDT.Rows)
        {              
           splitINT = row[columnName].GetHashCode();
           tmp = splitINT % mod;
           splitDT[tmp].ImportRow(row);
           MessageBox.Show("value:" + row[columnName].ToString() + "splitINT:" + splitINT + "mod:" + mod +
                            " to table:" + tmp);
            MessageBox.Show("" + splitDT[tmp].Rows.Count);
        }

        return splitDT;
    }
8
  • Be sure that the first method call return datatable[] with data. Commented Jun 4, 2013 at 11:55
  • Have you tried your first method? Commented Jun 4, 2013 at 11:58
  • @Steve i've added a try to show row count in each split table... they aren't empty Commented Jun 5, 2013 at 5:21
  • I wish to see how do you call the first method. The one that is supposed to fill the splitDT array. Do you have a try/catch empty handler around the call? Commented Jun 5, 2013 at 7:18
  • Still I can't see how do you fill the DataTable mainDT passed to the method splitTable. However that method could not work because GetHashCode return a number that cannot be a number between 0 and 10 needed to add something to your splitDT array Commented Jun 5, 2013 at 18:51

3 Answers 3

1

The ImportRow on a DataTable without schema doesn't produce any result.

public DataTable[] splitTable(DataTable mainDT,string columnName,int mod)
{
    DataTable[] splitDT = new DataTable[11];
    for (int i=0;i<11;i++)
    {
        // Create a datatable with the same structure (schema) of the source table
        splitDT[i] = mainDT.Clone();
    }

    int splitINT;
    int tmp=0;

    foreach (DataRow row in mainDT.Rows)
    {              
       splitINT = row[columnName].GetHashCode();
       tmp = splitINT % mod;
       splitDT[tmp].ImportRow(row);
    }

    return splitDT;
}

This code copies only a column, not the Whole set of columns. Perhaps your code should create a datatable with only the column to copy from.

Sign up to request clarification or add additional context in comments.

1 Comment

used this code, column number OK, still can't show it in the tabcontrol tab
0

Because your dgw size is 0,0 either set size:

dgw.Size = new Size(100, 100);

or set dock type to fill:

dgw.Dock = System.Windows.Forms.DockStyle.Fill;

Comments

0

In both places where you create the new DataGridViews, you have set the data-source - which will allow it to access the rows, but you haven't told it to handle the columns itself. Try setting AutoGenerateColumns to true before you set the DataSource; i.e.

DataGridView dgw = new DataGridView();
TabPage tp = new TabPage();

//data grid view
dgw.AutoGenerateColumns = true; // <====== added this line
dgw.Name = "dgv" + num;
dgw.AutoSize = true;
dgw.Dock = DockStyle.Fill;

// ... some here not shown

dgw.DataSource = dt; 

and:

dgw = new DataGridView();
dgw.AutoGenerateColumns = true; // <====== added this line
dgw.DataSource = left[i];

6 Comments

still blank grey datagridview
@iakovl2 what is dt.Columns.Count ? I'm wondering: are there actually any columns here?
I think the problem is in the DataTable without schema used to call ImportRow
@Steve - from the documentation of Load for scenario "The DataTable has no schema" : "The Load method infers the schema based on the result set from the imported IDataReader."; edit, ah I see what you mean - the OP's splitTable method
However, just tried with a simple example trying to ImportRow from a DataTable already filled (as here) and without the Clone the resulting DataTable.Columns.Count is zero
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.