error with datagridview C #

0

I have a problem trying to run this code, what I try to do is that in column 9 the "," and the "." are taken into account, the code is as follows:

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+textBox1.Text+";Extended Properties=Excel 12.0");

OleDbDataAdapter da= new OleDbDataAdapter("SELECT * FROM [Hoja1$]", con);
DataTable dt = new DataTable();
da.Fill(dt);

dataGridView1.Columns[9].ValueType = typeof(decimal);
dataGridView1.Columns[9].DefaultCellStyle.FormatProvider = ci;
dataGridView1.Columns[9].DefaultCellStyle.Format = "N2";
dataGridView1.DataSource = dt;

The error you send me is as follows:

  

index was out of range. Must be non-negative and less than the size   of the collection.

The table contains 18 columns, so I do not know why this error appears.

    
asked by oscar ramirez 19.02.2017 в 06:34
source

1 answer

0

Because your dataset already generates the datagrid schema, so it is not necessary to redefine the schema, only in your datagridview generates the following event:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (dataGridView1.Columns[e.ColumnIndex].Name == "NombreDeColumnaAFormatear")
        {
            e.CellStyle.Format = "N2"; //Formato como N2, C4, etc.
        }
    }

Note: The name of the column would be the name from the header of your sql query

    
answered by 20.02.2017 / 17:19
source