Snooke 87 Posted October 9, 2016 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: Erro sem o IF: Help? Abraço, André Share this post Link to post Share on other sites
MiguelSousa 55 Posted October 9, 2016 Vê se é isto que queres.Se for:"CopyPasta" and done.http://imgur.com/a/DbJ8Yhttp://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
Snooke 87 Posted October 9, 2016 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
MiguelSousa 55 Posted October 9, 2016 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
Snooke 87 Posted October 9, 2016 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
Cσяvσ⋆ 1,804 Posted October 9, 2016 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
Snooke 87 Posted October 9, 2016 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