How to pass a Textbox value to Datagridview

Asked

Viewed 2,125 times

-1

How to pass what is written inside a TextBox.Text for a DataGridView? and how to save what is in DataGridViewin an archive .TXT?

1 answer

0


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

To access the value of Textbox just 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

To the Datagridview it’s simple, you just need to assign where you want, Ex to First row and first column:

C#


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

VB


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

Being Rows the Lines and Cells columns, if you want to go through Datagridview can do as follows:(You can also use Foreach but I preferred 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 for a . txt as perfectly described Adam White In this Question can use a hack by copying all information using a native copy function from Datagridview instead of going all the way

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
  • thanks for the reply, take a look at the profile, I am now wrapped in another question kkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.