winform 的一些记录
1、winform 播放语音
System.Media.SoundPlayer player = new System.Media.SoundPlayer(); player.SoundLocation = Application.StartupPath + "//请到此处报道.wav"; player.LoadAsync(); //异步读取 //player.PlaySync();//异步播放 //player.Load(); player.PlayLooping();//循环播放
2、DataGridViewComboBoxCell 绑定值
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { var cell = this.dataGridView1.Rows[e.RowIndex].Cells[1]; DataGridViewComboBoxCell DgvCell = this.dataGridView1.Rows[e.RowIndex].Cells[1] as DataGridViewComboBoxCell; DgvCell.DataSource = comboboxOption; DgvCell.ValueMember = "pointType"; DgvCell.DisplayMember = "Description"; }
3、DataGridViewComboBoxCell 下拉框事件
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { DataGridView dgv = sender as DataGridView; if (dgv.CurrentCell.GetType().Name == "DataGridViewComboBoxCell" && dgv.CurrentCell.RowIndex != -1) { //加上下拉事件 (e.Control as ComboBox).SelectedIndexChanged += PointType_SelectedIndexChanged; } } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PointType_SelectedIndexChanged(object sender, EventArgs e) { ComboBox combox = sender as ComboBox; combox.Leave += Combox_Leave; try { if (combox.SelectedItem != null && dataGridView.SelectedRows.Count > 0) { var row = dataGridView1.CurrentRow; Flyline fl = Client.getSide().Flylines.FirstOrDefault(x => x.guid == dataGridView.SelectedRows[0].Cells["ID"].Value.ToString()); if (fl != null) { LinePoint lp = fl.LinePoints.FirstOrDefault(x => x.ID == row.Cells["ID1"].Value.ToString()); if (lp != null) { var pointType = combox.SelectedValue; if (pointType != null && !string.IsNullOrWhiteSpace(pointType.ToString())) { lp.pointType = pointType.ToString(); } } } } Thread.Sleep(100); } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// <summary> /// 离开combox的时候,把事件删除 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Combox_Leave(object sender, EventArgs e) { ComboBox combox = sender as ComboBox; //做完处理,撤销动态事件 combox.SelectedIndexChanged -= PointType_SelectedIndexChanged; }
4、再次加载的时候绑定数据
var data = Client.FirstOrDefault(x => x.guid == dataGridView.SelectedRows[0].Cells["ID"].Value.ToString()).LinePoints; dataGridView1.DataSource = data; for (var i = 0; i < data.Count; i++) { ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[1]).DataSource = comboboxOption; ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[1]).ValueMember = "pointType"; ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[1]).DisplayMember = "Description"; ((DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[1]).Value = data[i].pointType; }