Giter Site home page Giter Site logo

Comments (5)

davidegironi avatar davidegironi commented on June 7, 2024

Hello,
That must be something related to your datatable, so to your elemento.Value

If you try building it with a sample datatable it works. I see you cast the Value to a DataTable, is it a DataTable?

Find below a test code that I've tested.

public void createDGV(AdvancedDataGridView dgv)
        {
            // sample datatable
            DataTable dataTable = new DataTable("test");
            dataTable.Columns.Add("col1", typeof(int));
            dataTable.Columns.Add("col2", typeof(string));

            dgv.Dock = DockStyle.Fill;

            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = dataTable;
            dgv.DataSource = bindingSource;

            dgv.RowHeadersVisible = false;
            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
            dgv.AllowUserToAddRows = false;
        }

I've try to replicate your

from advanceddatagridview.

rhandalfm avatar rhandalfm commented on June 7, 2024

Hello David, thanks so much for your answer!

I work closely with Layla and continued digging into the issue as per your guidance.
I confirm to you that the Value is indeed exactly a DataTable.

So what I did was instead of inserting my DataTable, I created a new one at the same time and I kept having the same problem,

    public void createDGV(KeyValuePair<string, object> elemento, AdvancedDataGridView dgv)
    {
        dgv.Dock = DockStyle.Fill;
        DataTable dt = new DataTable();
       
        DataTable dataTable = new DataTable("test");
        dataTable.Columns.Add("col1", typeof(int));
        dataTable.Columns.Add("col2", typeof(string));
        dt = dataTable;

        BindingSource bindingSource = new BindingSource();
        try
        {
            bindingSource.DataSource = dt;
            dgv.DataSource = bindingSource; // Here's the error
        }
        catch { }

        dgv.RowHeadersVisible = false;
        dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
        dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
        dgv.AllowUserToAddRows = false;

        dgv.SortStringChanged += (sender, e) => dgv_Dados_SortStringChanged(sender, e, bindingSource);
        dgv.FilterStringChanged += (sender, e) => dgv_Dados_FilterStringChanged(sender, e, bindingSource);
    }

I'm suspecting that the problem is in inserting several ADGV in different TabPages in the same TabControl.

As shown in the code below, for each report, I create a new ADGV and a new TabPage, and insert it into the existing TabControl.

The error in the code only appears when inserting the second report.

    foreach(var elemento in resultsList)
        {
            var tab = new TabPage();
            tab.Name = "tab_" + elemento.Key;
            tab.Text = elemento.Key;
            tab_Results.Controls.Add(tab);
            tab.Padding = new Padding(4, 4, 4, 4);
            tab.BackColor = Color.White;
            AdvancedDataGridView dgv = new AdvancedDataGridView();
            dgv.ReadOnly = true;
            tab.Controls.Add(dgv);
            createDGV(elemento, dgv);
        }

It would be really appreciated if you could help us one more time.

Kind regards

from advanceddatagridview.

davidegironi avatar davidegironi commented on June 7, 2024

Hello @rhandalfm
I'm using several ADGV with no problem. Can you build a sample project?

from advanceddatagridview.

rhandalfm avatar rhandalfm commented on June 7, 2024

Helli @davidegironi !

Thanks again for your help and for responding quickly.

I managed to reproduce the problem in this code below.
https://github.com/rhandalfm/ADGVTest

Reinforcing that the error only happens when inserting the second tab.

With this part of the code inside the try catch, the code continues to throw the error but manages to run to the end (most of the time) and insert the ADGVs in the tabs.

        try
        {
            bindingSource.DataSource = dados;
            dgv.DataSource = bindingSource;  // Here's the error
        }
        catch
        {
            ;
        }

Kind regards.

from advanceddatagridview.

davidegironi avatar davidegironi commented on June 7, 2024

Hello,

At first you have to create it on FormLoad event, then you have to follow the following order:

  1. create the dgv
  2. add to control
  3. select the tab (this will give all the underline event a dgv a shot)
  4. custom set the dgv columns

Find your form1.cs code modified below:

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Zuby.ADGV;

namespace ADGVsample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            test();
        }
        private void test()
        {
            for (int i = 0; i < 3; i++)
            {
                var tab = new TabPage();
                tab.Name = "tab_" + i.ToString();
                tab.Text = i.ToString();
                tab_Resultado.Controls.Add(tab);
                tab.Padding = new Padding(4, 4, 4, 4);
                tab.BackColor = Color.White;

                AdvancedDataGridView dgv = new AdvancedDataGridView();
                dgv.ReadOnly = true;
                createDGV(dgv);
                tab.Controls.Add(dgv);
                tab_Resultado.SelectedTab = tab;
                setDGVcolumns(dgv);
            }
        }
        public void createDGV(AdvancedDataGridView dgv)
        {
            dgv.Dock = DockStyle.Fill;
            DataTable dados = new DataTable();

            dados.Columns.Add("col1", typeof(int));
            dados.Columns.Add("col2", typeof(string));

            BindingSource bindingSource = new BindingSource();
            try
            {
                bindingSource.DataSource = dados;
                dgv.DataSource = bindingSource;
            }
            catch
            {
                ;
            }

            dgv.RowHeadersVisible = false;
            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
            dgv.AllowUserToAddRows = false;

            dgv.SortStringChanged += (sender, e) => dgv_Dados_SortStringChanged(sender, e, bindingSource);
            dgv.FilterStringChanged += (sender, e) => dgv_Dados_FilterStringChanged(sender, e, bindingSource);
        }

        public void setDGVcolumns(AdvancedDataGridView dgv)
        {
            DataTable dados = (DataTable)((dgv.DataSource as BindingSource).DataSource);
            foreach (DataColumn coluna in dados.Columns)
            {
                if (coluna.DataType == typeof(long) || coluna.DataType == typeof(int))
                {
                    dgv.Columns[coluna.ColumnName].DefaultCellStyle.Format = "N0";
                    dgv.Columns[coluna.ColumnName].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                }
                else
                if (coluna.DataType == typeof(double))
                {
                    dgv.Columns[coluna.ColumnName].DefaultCellStyle.Format = "N2";
                    dgv.Columns[coluna.ColumnName].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                }
            }
        }

        private void dgv_Dados_SortStringChanged(object sender, EventArgs e, BindingSource bindingSource)
        {
            bindingSource.Sort = ((AdvancedDataGridView)sender).SortString;
        }

        private void dgv_Dados_FilterStringChanged(object sender, EventArgs e, BindingSource bindingSource)
        {
            bindingSource.Filter = ((AdvancedDataGridView)sender).FilterString;
        }
    }
}

from advanceddatagridview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.