none
Primeiro registro do select nao aparece RRS feed

  • Pergunta

  • Boa Noite

    estou tentando popular um grid e tenho 3 registros no banco , mas quando exibo o form so aparecem 2 registros , percebi que é o primeiro que nao aparece ..

    Alguem ja teve esse problema , abaixo segue o codigo e estou fazendo em windows forms com C#

    Obrigado

    // DataSet dt = new DataSet();
          DataTable dt = new DataTable();
          string Constr = @"Data Source=.\sqlexpress;AttachDbFilename=C:\Cobranca\Dados\Juridico.mdf;Integrated Security=True";
          ///  string Constr = @"Data Source=htw\sqlexpress;AttachDbFilename=C:\Cobranca2\Dados\Juridico.mdf;Integrated Security=True";
          SqlConnection Conn = new SqlConnection(Constr);
          SqlCommand cmd = new SqlCommand("select IdEndereco, logradouro, bairro, cidade, UF, CEP from Enderecos order by Logradouro ", Conn);
          Conn.Open();
          SqlDataReader dtr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
          dtr.Read();
          dt.Load(dtr);
    
    
            dataGridView1.DataSource = dt;
            dataGridView1.Sort(dataGridView1.Columns[1], System.ComponentModel.ListSortDirection.Ascending);
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            dataGridView1.Columns[0].HeaderText = "CÓDIGO";
            dataGridView1.Columns[1].HeaderText = "LOGRADOURO";
            dataGridView1.Columns[2].HeaderText = "BAIRRO";
            dataGridView1.Columns[3].HeaderText = "CIDADE";
            dataGridView1.Columns[4].HeaderText = "UF";
            dataGridView1.Columns[5].HeaderText = "CEP";
            style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            style.Font = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Pixel);
            dataGridView1.Columns[0].HeaderCell.Style = style;
            dataGridView1.Columns[1].HeaderCell.Style = style;
            dataGridView1.Columns[2].HeaderCell.Style = style;
            dataGridView1.Columns[3].HeaderCell.Style = style;
            dataGridView1.Columns[4].HeaderCell.Style = style;
            dataGridView1.Columns[5].HeaderCell.Style = style;
            dataGridView1.Columns[0].Width = 50;
            dataGridView1.Columns[1].Width = 300;
            dataGridView1.Columns[2].Width = 200;
            dataGridView1.Columns[3].Width = 200;
            dataGridView1.Columns[4].Width = 50;
            dataGridView1.Columns[5].Width = 90;
            textBox1.Focus();
    

     

    sexta-feira, 27 de maio de 2011 23:41

Respostas

  • Esquece datareader, Voce nao esta a fazer pesquiza:

    string teste = (string)("SELECT * From Tabela");

                String cnn = tua conection string aqui;

                System.Data.SqlClient.SqlConnection conetcme = new System.Data.SqlClient.SqlConnection(cnn.ToString());

                System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(teste, conetcme);

     

                DataSet DTS = new DataSet("Table_Copy");

                try

                {

                    conetcme.Open();

                    System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(command);

                    adapter.Fill(DTS);

     

                    this.datagridview.DataSource = DTS.Tables[0];

                }

     

                catch (System.Data.SqlClient.SqlException ex)

                {

                    MessageBox.Show(ex.ToString());

     

                }

                conetcme.Close();

     

            }


    One word frees us of all the weight and pain of life: that word is love.
    • Marcado como Resposta Mr.Been sábado, 28 de maio de 2011 00:22
    sábado, 28 de maio de 2011 00:03