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?
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?
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