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;
    }
iakovl2
  • 101
  • 2
  • 3
  • 7
  • Be sure that the first method call return datatable[] with data. – kostas ch. Jun 04 '13 at 11:55
  • Have you tried your first method? – Steve Jun 04 '13 at 11:58
  • @Steve i've added a try to show row count in each split table... they aren't empty – iakovl2 Jun 05 '13 at 05: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? – Steve Jun 05 '13 at 07: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 – Steve Jun 05 '13 at 18:51
  • @Steve thats why there is a "%" a mod operation making it when i make mod 11 to be 1-10 – iakovl2 Jun 05 '13 at 18:53
  • @iakovl2 `GetHashCode` can return a negative number; a `%` on a negative number is ***still negative*** (`%` does not guarantee to return `0` to `n-1`). Frankly, I see no sane reason to use `GetHashCode` here : that is **not** right for what you are doing here – Marc Gravell Jun 06 '13 at 10:16
  • @MarcGravell , the values are mostly int only so gethash on int gives the same int, in some cases it could be string so hash should still be positive, the "mod" makes the index in the splitDT datatable array – iakovl2 Jun 06 '13 at 10:22

3 Answers3

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.

Steve
  • 213,761
  • 22
  • 232
  • 286
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;
gzaxx
  • 17,312
  • 2
  • 36
  • 54
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];
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • still blank grey datagridview – iakovl2 Jun 06 '13 at 09:51
  • 1
    @iakovl2 what is `dt.Columns.Count` ? I'm wondering: are there actually any columns here? – Marc Gravell Jun 06 '13 at 09:55
  • I think the problem is in the DataTable without schema used to call ImportRow – Steve Jun 06 '13 at 09:56
  • @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 – Marc Gravell Jun 06 '13 at 09:58
  • 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 – Steve Jun 06 '13 at 10:03
  • @Steve see added code, has multiple Messegeboxes to show value of "sutff" the problem it show the info. i just can't get to show it right – iakovl2 Jun 06 '13 at 10:10