https://www.facebook.com/photo.php?fbid=494142207337085&set=a.102999783117998.6074.100002239302471&type=1&theater如何將數據從文本保存到dartagridview
我們的目標是從串口發送數據到文本框,以及在數據網格視圖。我們應該怎麼做?任何意見或建議將不勝感激。順便說一句,我們使用的是C#的Visual Studio 2012
string RxString;
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM71";
serialPort1.BaudRate = 9600;
serialPort1.Open();
if (serialPort1.IsOpen)
{
buttonStart.Enabled = false;
buttonStop.Enabled = true;
dataGridView1.Rows.Add(10);
}
}
private void buttonStop_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
buttonStart.Enabled = true;
buttonStop.Enabled = false;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!serialPort1.IsOpen) return;
char[] buff = new char[1];
buff[0] = e.KeyChar;
serialPort1.Write(buff, 0, 1);
}
private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(RxString);
}
private void serialPort1_DataReceived
(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
dataGridView1.Rows[0].Cells[0].Value = textBox1.Lines[0];
}
}
}
我不知道如何將數據從文本在DataGridView轉移到每一行。請幫助:'(
@codeswithhammer
香港專業教育學院使用這這些代碼,但仍然沒有工作,它說的是方法必須返回類型
private DataTable myTable = new DataTable();
public Form2()
{
InitializeComponent();
// initialise
this.initialiseTable(this.myTable);
// set source
this.dataGridView1.DataSource = this.myTable;
}
/// <summary>
/// Initialise a table
/// </summary>
/// <param name="table">The table to initialise</param>
private void initialiseTable(DataTable table)
{
// add all columns
table.Columns.Add(new DataColumn("Column1"));
table.Columns.Add(new DataColumn("Column2"));
table.Columns.Add(new DataColumn("Column3"));
}
private void button1_Click(object sender, EventArgs e)
{
// create a row
DataRow rd = this.myTable.NewRow();
// set values
rd[0] = textBox1.Text;
rd[1] = textBox2.Text;
rd[2] = textBox3.Text;
// add row to table
this.myTable.Rows.Add(rd);
}
}
}
我只是有什麼上面顯示的圖片文件
您的應用程序現在從文本框中寫入數據到一個串口,所以它的輸出數據,但我因子評分你想從獲取輸入一個來源,並將數據放入一個文本框? – Max
@MaxMommersteeg我希望textbow中顯示的數據位於gridview中。我已經讓它有文本框中的數據 –