How to pass a value from the Textbox to DataGridView [closed]

-1

How to pass what is written inside a TextBox.Text to a DataGridView ? and how to save what is in DataGridView to a .TXT file?

    
asked by anonymous 26.04.2017 / 05:22

1 answer

0

After answering I noticed that the question was edited in the meantime and the C # tag Removed leaving only VB.net, I took the liberty of convert the codes I wrote in C # for Vb so leaving the 2 versions

To access the TextBox value, simply use the command you described yourself TextBox.Text Ex:

C #

String A = TextBox.Text;
//A = Texto do TextBox

VB

Dim A As [String] = TextBox.Text
'A = Texto do TextBox

For the DataGridView is simple, you only need to assign where you want, Ex for First line and first column:

C #

DataGridView.Rows[0].Cells[0].Value = TextBox.Text;

VB

DataGridView.Rows(0).Cells(0).Value = TextBox.Text

Rows and Cells columns, if you want to go through DataGridView you can do the following: (You can also use Foreach but I preferred the While for better understanding)

C #

 int L = 0;//Linhas
 int C = 0;//Colunas
 while (DataGridView.Rows.Count > L)
 {
    C = 0;
    while (DataGridView.Columns.Count > C)
    {
      DataGridView.Rows[0].Cells[0].Value = TextBox.Text;//Altere o valor a ser recebido para oque desejar
      C++;
    }
    L++;
 }

VB

Dim L As Integer = 0'Linhas
Dim C As Integer = 0'Colunas
While DataGridView.Rows.Count > L
    C = 0
    While DataGridView.Columns.Count > C
        DataGridView.Rows(0).Cells(0).Value = TextBox.Text
        'Altere o valor a ser recebido para oque desejar
        C += 1
    End While
    L += 1
End While

And to convert the DataGridView to a .txt as perfectly described Adam White In this Question you can use of a hack by copying all information using a native copy function of the DataGridView instead of going through it all

C #

private void SaveDataGridViewToCSV(string Filename)
{
    // Se quiser que os nomes das colunas tambem sejam copiados utilize EnableWithoutHeaderText como abaixo;
    dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithAutoHeaderText;
    // Seleciona todas as Celulas
    dataGridView1.SelectAll();
    // Copy (seta para o CTRL+C)
    Clipboard.SetDataObject(dataGridView1.GetClipboardContent());
    // Paste (Pega o CTRL+V e converte para um arquivo)
    File.WriteAllText(Filename,Clipboard.GetText(TextDataFormat.CommaSeparatedValue));
}

VB

Private Sub SaveDataGridViewToCSV(Filename As String)
' Se quiser que os nomes das colunas tambem sejam copiados utilize EnableWithoutHeaderText como abaixo;
dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithAutoHeaderText
' Seleciona todas as Celulas
dataGridView1.SelectAll()
' Copy (seta para o CTRL+C)
Clipboard.SetDataObject(dataGridView1.GetClipboardContent())
' Paste (Pega o CTRL+V e converte para um arquivo)
File.WriteAllText(Filename, Clipboard.GetText(TextDataFormat.CommaSeparatedValue))
End Sub
    
26.04.2017 / 13:53