Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 747955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:15:53+00:00 2026-05-14T14:15:53+00:00

Im adding objects to a datagridview ( only one kind) through a list ej.

  • 0

Im adding objects to a datagridview ( only one kind) through a list
ej.

List<Material> mater = new List<Material>();
DataGridView dgvMAterial = new DataGridView();

dgvMaterial.DataSource = null;
mater.Add((Material)cmbMaterial.SelectedValue);
dgvMaterial.DataSource = mater;


But every time I click over the datagrid I get an indexoutofrangeexeption.
Can somone tell me why?
thanks

here is my whole code for the form

public partial class inicio : Form
{
    private string ConnectionString = "Data Source=localhost\\sqlexpress;Initial Catalog=data.mdf;Integrated Security=SSPI;";
    //private string ConnectionString = "Server=.\\SQLExpress;AttachDbFilename=|DataDirectory|\\data\\data_data.mdf.mdf; Database=data.mdf;Trusted_Connection=Yes;";
    private ISessionFactory sessionFactory;
    List<Material> mater = new List<Material>();
    List<Salarios> salar = new List<Salarios>();
    IBindingList mind = new BindingList<Salarios>();
    Productos prod;

    public inicio()
    {
        InitializeComponent();
        sessionFactory = nhn.BusinessObjects.Initialize.CreateSessionFactory(ConnectionString);
        dgvMaterial.DataSource = mater;
    }

    private void materialToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Catalogos.frmMaterial material = new costeos.Catalogos.frmMaterial(ConnectionString);
        material.ShowDialog(this);
        material.Dispose();
    }

    private void salariosToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Catalogos.frmSalarios salarios = new costeos.Catalogos.frmSalarios(ConnectionString);
        salarios.ShowDialog(this);
        salarios.Dispose();
    }

    private void agregarToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Catalogos.frmAddRemuneraciones rem = new costeos.Catalogos.frmAddRemuneraciones(ConnectionString);
        rem.ShowDialog(this);
        rem.Dispose();
    }

    private void agregarToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        Catalogos.frmAddAdmin adm = new costeos.Catalogos.frmAddAdmin(ConnectionString);
        adm.ShowDialog(this);
        adm.Dispose();
    }

    private void agregarToolStripMenuItem2_Click(object sender, EventArgs e)
    {
        Catalogos.frmAddInsumosInd insumos = new costeos.Catalogos.frmAddInsumosInd(ConnectionString);
        insumos.ShowDialog(this);
        insumos.Dispose();
    }

    private void txt_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (char.IsDigit(e.KeyChar) || char.IsPunctuation(e.KeyChar) || char.IsControl(e.KeyChar))
        {
            e.Handled = false;
        }
        else
        {
            e.Handled = true;
        }
    }

    private void inicio_Load(object sender, EventArgs e)
    {
        LlenaCampos();
    }

    private void LlenaCampos()
    {
        using (var session = sessionFactory.OpenSession())
        {
            using (var transaction = session.BeginTransaction())
            {
                var mat = session.CreateCriteria(typeof(Material))
                    .List<Material>();
                var sal = session.CreateCriteria(typeof(Salarios))
                    .List<Salarios>();
                transaction.Commit();

                cmbMaterial.DataSource = mat;
                cmbMaterial.DisplayMember = "Nombre";
                cmbSalarios.DataSource = sal;
                cmbSalarios.DisplayMember = "Nombre";
                cmbMIndirecta.DataSource = sal;
                cmbMIndirecta.DisplayMember = "Nombre";
            }
        }
    }

    private void btnAddMaterial_Click(object sender, EventArgs e)
    {
        materialBindingSource.DataSource = null;
        //dgvMaterial.DataSource = null;
        mater.Add((Material)cmbMaterial.SelectedValue);
        //dgvMaterial.DataSource = mater;
        dgvMaterial.DataSource = materialBindingSource;
        materialBindingSource.DataSource = mater;
        materialBindingSource.ResetBindings(false);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        dgvSalarios.DataSource = null;
        salar.Add((Salarios)cmbSalarios.SelectedValue);
        dgvSalarios.DataSource = salar;
    }

    private void button3_Click(object sender, EventArgs e)
    { 
        dgvMIndirecta.DataSource = null;
        mind.Add((Salarios)cmbMIndirecta.SelectedValue);
        dgvMIndirecta.DataSource = mind;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        using (var session = sessionFactory.OpenSession())
        {
            using (var transaction = session.BeginTransaction())
            {
                if (prod == null)
                {
                    prod = new Productos { CargasTurno = float.Parse(txtCargasTurno.Text), CavidadesMolde = int.Parse(txtCavidadesMolde.Text), Clave = txtClave.Text, Comentarios = txtComentarios.Text, MezclasTurno = float.Parse(txtMezclasTurno.Text), Moldes = int.Parse(txtMoldes.Text), Nombre = txtNombre.Text, Peso = float.Parse(txtPesoTotal.Text), TotalPza = int.Parse(txtPzasTotales.Text), Turnos = int.Parse(txtTurnos.Text) };
                    session.Save(prod);
                    transaction.Commit();
                }
                foreach (DataGridViewRow dr in dgvMaterial.Rows)
                {
                    Material m = dr.DataBoundItem as Material;
                    m.Materiales 
                    PMaterial mat = new PMaterial { Material = dr.DataBoundItem as Material, Cantidad = float.Parse(dr.Cells["Cantidad"].Value.ToString()), Fecha = DateTime.Now, Producto = prod };
                    session.Save(mat);
                }
                transaction.Commit();
                session.Close();
            }
        }
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:15:53+00:00Added an answer on May 14, 2026 at 2:15 pm

    I would guess that you have an event-handler that isn’t happy. What exactly does the message say?

    You might also be having problems if you are adding the same Material instance to the list multiple times; since IndexOf will only find the first occurrence. This line makes me very suspicious:

    mater.Add((Material)cmbMaterial.SelectedValue);
    

    since it could potentially (on consecutive clicks / etc) do exactly this.


    Note: if you used BindingList<T> instead all you’d have to doo is Add(...) – no resetting required:

    field:

    BindingList<Material> mater = new BindingList<Material>();
    

    init grid:

    dgvMaterial.DataSource = mater;
    

    add item:

    mater.Add(newInstance);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 448k
  • Answers 448k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've tried loading the page with javascript disabled in chrome,… May 15, 2026 at 7:44 pm
  • Editorial Team
    Editorial Team added an answer Take a look at the bottom of section 14.9 of… May 15, 2026 at 7:44 pm
  • Editorial Team
    Editorial Team added an answer You can attache an event handler to the Navigating event.… May 15, 2026 at 7:44 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.