antiblock
https://arwen2.global/
  • Chatbox

    You don't have permission to chat.
    Load More
Sign in to follow this  
Snooke

[Duvida] Adicionar Mais Do Que 1 Registo

7 posts in this topic

Olá.


Ainda à pouco estive aqui a fazer um tópico sobre adicionar registos importados de ficheiro de texto para a datagridview e já consegui resolver +/-, fiz um "upgrade" no código e ele só aceita apenas 8 linhas, se colocar mais ele rebenta.


Adicionei um if, para ele não bugar, só para correção , mas só aceita 1 registo. Se tirar o If e colocar + do que 1 registo ele vai rebentar dando o seguinte erro:


 


Código com o IF:


0lm.png


 


Erro sem o IF:


0lP.png


 


Help?


 


Abraço,


André


Share this post


Link to post
Share on other sites
antiblock
https://arwen2.global/

Vê se é isto que queres.

Se for:
"CopyPasta" and done.

http://imgur.com/a/DbJ8Y
http://imgur.com/a/SBcsu

http://imgur.com/a/VfpXm

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] lines = File.ReadAllLines("C:\\teste.txt");
            int i = 0;
            foreach (string l_txt in lines)
            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows[i].Cells[0].Value = l_txt;
                i++;
            }
        }


Se não for:
404 error: File not found

Share this post


Link to post
Share on other sites

Vê se é isto que queres.

Se for:

"CopyPasta" and done.

http://imgur.com/a/DbJ8Y

http://imgur.com/a/SBcsu

http://imgur.com/a/VfpXm

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] lines = File.ReadAllLines("C:\\teste.txt");
            int i = 0;
            foreach (string l_txt in lines)
            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows[i].Cells[0].Value = l_txt;
                i++;
            }
        }

Se não for:

404 error: File not found

Pelo contrário, é algo assim http://prntscr.com/crtlqk

 

Obrigado na mesma !!

Share this post


Link to post
Share on other sites

Nesse caso:

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToAddRows = false;
            string[] lines = File.ReadAllLines("C:\\teste.txt");
            int i = 0;
            dataGridView1.Rows.Add(); // Comentar para várias linhas
            foreach (string l_txt in lines)
            {
                //dataGridView1.Rows.Add(); // Descomentar para várias linhas
                dataGridView1.Rows[0].Cells[i].Value = l_txt;
                i++;
            }
        }

http://imgur.com/a/xZg0O

 

http://imgur.com/a/6R7LW

Share this post


Link to post
Share on other sites

Nesse caso:

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToAddRows = false;
            string[] lines = File.ReadAllLines("C:\\teste.txt");
            int i = 0;
            dataGridView1.Rows.Add(); // Comentar para várias linhas
            foreach (string l_txt in lines)
            {
                //dataGridView1.Rows.Add(); // Descomentar para várias linhas
                dataGridView1.Rows[0].Cells[i].Value = l_txt;
                i++;
            }
        }

http://imgur.com/a/xZg0O

 

http://imgur.com/a/6R7LW

O problema desse é que quando tenho mais do que 1 registo no txt e importo o programa rebenta nesta linha dataGridView1.Rows[0].Cells.Value = l_txt;

Fazes a ideia porque?

Share this post


Link to post
Share on other sites

O problema desse é que quando tenho mais do que 1 registo no txt e importo o programa rebenta nesta linha dataGridView1.Rows[0].Cells.Value = l_txt;

Fazes a ideia porque?

private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToAddRows = false;
            string[] lines = File.ReadAllLines("C:\\teste.txt");
            int i = 0;
            foreach (string l_txt in lines)
            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows[0].Cells[i].Value = l_txt;
                i++;
            }
        }

Share this post


Link to post
Share on other sites
private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToAddRows = false;
            string[] lines = File.ReadAllLines("C:\\teste.txt");
            int i = 0;
            foreach (string l_txt in lines)
            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows[0].Cells[i].Value = l_txt;
                i++;
            }
        }

Ele rebenta :| http://prntscr.com/crts8m

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this