- 最後登錄
- 2024-4-19
- 在線時間
- 0 小時
- 註冊時間
- 2016-8-22
- 閱讀權限
- 20
- 精華
- 0
- UID
- 16624936
- 帖子
- 61
- 積分
- 49 點
- 潛水值
- 26770 米
| 若新密碼無法使用,可能是數據未更新。請使用舊密碼看看。 以下給您參考- public Form1()
- {
- InitializeComponent();
- //按鈕繫結事件
- this.btn_Add.Click+=new EventHandler(btn_Add_Click);
- }
- //新增List
- List<Class1> list = new List<Class1>();
- //建立類別
- class Class1
- {
- public string property1 { get; set; }
- public string property2 { get; set; }
- }
- void btn_Add_Click(object sender, EventArgs e)
- {
- //檢查TextBox是否有輸入
- if (string.IsNullOrWhiteSpace(txt_property1.Text))
- {
- MessageBox.Show("請輸入項目1");
- return;
- }
- if (string.IsNullOrWhiteSpace(txt_property2.Text))
- {
- MessageBox.Show("請輸入項目2");
- return;
- }
- //把dataGridView資料來源清空
- this.dataGridView1.DataSource = null;
- //新增項目到List
- list.Add(new Class1() { property1=txt_property1.Text.Trim(), property2=txt_property2.Text.Trim() });
- //把List繫結到dataGridView
- this.dataGridView1.DataSource = list;
- //把List繫結到TextBox
- var temp = from item in list
- select item.property1+" " +item.property2;
- this.txt_Result.Text = string.Join(Environment.NewLine, temp.ToArray());
- }
- }
複製代碼 ... |
|