Вопрос

Ok so I have a function to search the DB for a record based on a number, then display it in the datagrid, this used to work fine but for some reason when I copied the code over sometime ago it now will bring a row back for every column in the DB (with the correct data). for example row 1 will just display a row in the datagrid but only with the first column displaying, then the second row will have the first two columns and so on....

Here is my code anyway

        dataGridView1.DataSource = null;
        dataGridView1.Rows.Clear();
        dataGridView1.Refresh();

        string strprovider = @"BLANKED OUT INFO HERE";
        string strsql = "Select * from OCR where OCR like '" + textBox4.Text + "%'";

        OleDbConnection newConn = new OleDbConnection(strprovider);
        OleDbCommand dbCmd = new OleDbCommand(strsql, newConn);

        newConn.Open();
        dbCmd.CommandType = CommandType.Text;
        OleDbDataReader dr = dbCmd.ExecuteReader();


        if (textBox4.Text == "")
        {
            MessageBox.Show("Please Enter a Valid OCR Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        else
        {

            if (dr.HasRows)
            {
                // ....
            }
            else
            {
                MessageBox.Show("No Record Found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            int columncount = dr.FieldCount;
            string[] rowdata = new string[columncount];
            while (dr.Read())
            {
                for (int k = 0; k < columncount; k++)
                {

                    switch (dr.GetFieldType(k).ToString())
                    {
                        case "System.Int32":
                            rowdata[k] = dr.GetInt32(k).ToString();
                            break;

                        case "System.DateTime":
                            rowdata[k] = dr.GetDateTime(k).ToString();
                            break;

                        case "System.String":
                            rowdata[k] = dr.GetString(k);
                            break;
                    }

                    dataGridView1.Rows.Add(rowdata);
                }

            }

        }

    }
Это было полезно?

Решение

Try adding your row after your for-loop:

        while (dr.Read())
        {
            for (int k = 0; k < columncount; k++)
            {

                switch (dr.GetFieldType(k).ToString())
                {
                    case "System.Int32":
                        rowdata[k] = dr.GetInt32(k).ToString();
                        break;

                    case "System.DateTime":
                        rowdata[k] = dr.GetDateTime(k).ToString();
                        break;

                    case "System.String":
                        rowdata[k] = dr.GetString(k);
                        break;
                }

                //dataGridView1.Rows.Add(rowdata); <= This shouldn't be here
            }
          //Add row after loop completes all columns
          dataGridView1.Rows.Add(rowdata);
        }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top