欧美一区2区三区4区公司二百,国产精品婷婷午夜在线观看,自拍偷拍亚洲精品,国产美女诱惑一区二区

DataGridView控件的用法詳解合集

1,當前選擇的的單元格屬性取得、變更
private void button3_Click(object sender, EventArgs e)
? ? ? ? {?
? ? ? ? ? ?
? ? ? ? ? ?//當前選擇的的單元格屬性取得、變更
? ? ? ? ? ? listBox1.Items.Add("當前選擇的表格值(代碼:dataGridView1.CurrentCell.Value)=" + dataGridView1.CurrentCell.Value);
? ? ? ? ? ? listBox1.Items.Add("當前選擇的表格值(代碼:dataGridView1.CurrentCell.ColumnIndex)=" + dataGridView1.CurrentCell.ColumnIndex);
? ? ? ? ? ? listBox1.Items.Add("當前選擇的表格值(代碼:dataGridView1.CurrentCell.RowIndex)=" + dataGridView1.CurrentCell.RowIndex);
? ? ? ? ? ? //設置[1,1]單元格為當前選擇的單元格
? ? ? ? ? ? dataGridView1.CurrentCell = dataGridView1[1,1];
? ? ? ? ? ? listBox1.Items.Add("當前選擇的表格值(代碼:dataGridView1.CurrentCell.RowIndex)=" + dataGridView1.CurrentCell);
? ? ? ? }
運行時,需要先按DataGridViewOpar ,它會創建DataGridView數據視圖實例,然后再按DataGridViewOparGather按鈕。

2,DataGridView編輯屬性
//全部單元格只讀
? ? ? ? ? ? dataGridView1.ReadOnly = true;
? ? ? ? ? ? //指定行列單元格只讀
? ? ? ? ? ? dataGridView1.Columns[1].ReadOnly = true;
? ? ? ? ? ? dataGridView1.Rows[2].ReadOnly = true;
? ? dataGridView1[0, 0].ReadOnly = true;
? ? ? ? ? //編輯指定單元格
private void dataGridView1_CellBeginEdit(object sender,
? ? DataGridViewCellCancelEventArgs e)
{
? ? string msg = String.Format("編輯表格 ({0}, {1})",
? ? ? ? e.ColumnIndex, e.RowIndex);
? ? this.Text = msg;
}
?
private void dataGridView1_CellEndEdit(object sender,
? ? DataGridViewCellEventArgs e)
{
? ? string msg = String.Format("完成編輯 ({0}, {1})",
? ? ? ? e.ColumnIndex, e.RowIndex);
? ? this.Text = msg;
}
3,DataGridView禁止用戶追加新行
dataGridView1.AllowUserToAddRows = false;
4,判斷當前選中行是否為新追加的行
if (dataGridView1.CurrentRow.IsNewRow = true)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("你選定的是新行");
? ? ? ? ? ? }
5,DataGridView設定刪除行
//允許用戶刪除行操作
dataGridView1.AllowUserToDeleteRows = true;
//雙擊DataGridView屬性框中事件列表中的以下兩個事件,添加代碼如下
//提示是否刪除指定行數據
? ? ? ? private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
? ? ? ? {
? ? ? ? ? ? DialogResult diaR = MessageBox.Show("是否刪除該行?", "確認", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
? ? ? ? ? ? if (diaR == DialogResult.OK)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? e.Cancel = false;
? ? ? ? ? ? }
}
//提示刪除了哪一行數據
? ? ? ? private void dataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
? ? ? ? {
? ? ? ? ? ? System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
? ? ? ? ? ? messageBoxCS.AppendFormat("{0} = {1}", "行號為", e.Row);
? ? ? ? ? ? messageBoxCS.AppendLine();
? ? ? ? ? ? DialogResult diaR = MessageBox.Show("刪除了" + messageBoxCS.ToString(), "確認");
? ? ? ? ? ?
? ? ? ? }
6,設置不顯示指定行,設置刪除選定的行或列
//顯示指定行或列
dataGridView1.Columns[0].Visible = false;
? ? ? ? ? ? dataGridView1.Rows[0].Visible = false;
? ? ? ? ? ? dataGridView1.ColumnHeadersVisible = false;
? ? ? ? ? ? dataGridView1.RowHeadersVisible = false;
?
? ? ? ? ? ? //刪除指定行
? ? ? ? ? ? dataGridView1.Columns.Remove("danwei");
? ? ? ? ? ? dataGridView1.Columns.RemoveAt(0);
? ? ? ? ? ? dataGridView1.Rows.RemoveAt(0);
//刪除選定的多行
? ? ? ? ? ? foreach( ?DataGridViewRow r in dataGridView1.SelectedRows)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (r.IsNewRow == false)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? dataGridView1.Rows.Remove(r);
? ? ? ? ? ? ? ? }
? ? ?}
7,取得選定的行、列、單元格
//選定的單元格
? ? ? ? ? ? foreach (DataGridViewCell c in dataGridView1.SelectedCells)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string cr = string.Format("{0},{1}", c.ColumnIndex, c.RowIndex);
? ? ? ? ? ? ? ? listBox1.Items.Add("選定的單元格位置是:" + cr);
? ? ? }
//選定的行/列
? ? ? ? ? ? foreach (DataGridViewRow c in dataGridView1.SelectedRows)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? listBox1.Items.Add("選定的行是:" + c.RowIndex);
? ? ?}
foreach (DataGridViewColumn c in dataGridView1.SelectedColumns)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? listBox1.Items.Add("選定的列是:" + c.ColumnIndex);
? ? ?}
? ? ? ? ?//指定選定單元格
? ? ? ? ? ? dataGridView1[0, 0].Selected = true;
? ? ? ? ?dataGridView1.Rows[0].Selected = true;
? ? ? ? ? ? dataGridView1.Columns[0].Selected = true;
?
//設置行首和左上角的文字
? ? ? ? ? ? dataGridView1.Rows[0].HeaderCell.Value = "第1行";
? ? ? ? ? ? dataGridView1.TopLeftHeaderCell.Value = "左上角";
8,手動追加列
//手動追加列
? ? ? ? ? ? dataGridView1.AutoGenerateColumns=false;
? ? ? ? ? ? dataGridView1.DataSource=ds;
? ? ? ? ? ? DataGridViewTextBoxColumn txtCol=new DataGridViewTextBoxColumn();
? ? ? ? ? ? txtCol.DataPropertyName="danwei";
? ? ? ? ? ? txtCol.Name="col1";
? ? ? ? ? ? txtCol.HeaderText="單位";
? ? ? ? ? ? dataGridView1.Columns.Add(txtCol);
9,單元格內輸入值正確性判斷
? ?在DataGridView控件的屬性處,選擇以下事件。
錯誤文本請求:
? ? ? ? private void dataGridView1_CellErrorTextNeeded(object sender, DataGridViewCellErrorTextNeededEventArgs e)
? ? ? ? {
? ? ? ? ? ? if ((dataGridView1.Columns[e.ColumnIndex].Name == "ID") && (dataGridView1.Columns["ID"].ToString()==""))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? dataGridView1.Rows[e.RowIndex].ErrorText="值類型錯誤";
? ? ? ? ? ? }
? ? ? ? }
? ? 輸入值有效性檢查:
? ? ? ? private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
? ? ? ? {
? ? ? ? ? ? dataGridView1.Rows[e.RowIndex].ErrorText="輸入值無效";
? ? ? ? }
10,列中顯示選擇框控件CheckBox
//列中顯示選擇框CheckBox
? ? ? ? ? ? DataGridViewCheckBoxColumn column1= new DataGridViewCheckBoxColumn();
? ? ? ? ? ?{
? ? ? ? ? ? ? ? column1.HeaderText = "選擇框";
? ? ? ? ? ? ? ? column1.Name = "checkbox";
? ? ? ? ? ? ? ? column1.AutoSizeMode =
? ? ? ? ? ? ? ? DataGridViewAutoSizeColumnMode.DisplayedCells;
? ? ? ? ? ? ? ? column1.FlatStyle = FlatStyle.Standard;
? ? ?//顯示選擇框的三種狀態
? ? ? ? ? ? ? ? ?column1.ThreeState = true;
? ? ? ? ? ? }
? ? ? ? ? ? dataGridView1.Columns.Add(column1);

?

文章鏈接: http://www.qzkangyuan.com/15118.html

文章標題:DataGridView控件的用法詳解合集

文章版權:夢飛科技所發布的內容,部分為原創文章,轉載請注明來源,網絡轉載文章如有侵權請聯系我們!

聲明:本站所有文章,如無特殊說明或標注,均為本站原創發布。任何個人或組織,在未征得本站同意時,禁止復制、盜用、采集、發布本站內容到任何網站、書籍等各類媒體平臺。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
建站教程投稿分享

設置共享文件夾在主機與本地VMware虛擬機之間傳輸文件

2022-12-29 23:42:30

建站教程投稿分享

CentOS 7.8 mini 初裝配置

2022-12-29 23:54:41

0 條回復 A文章作者 M管理員
    暫無討論,說說你的看法吧
?
個人中心
購物車
優惠劵
今日簽到
有新私信 私信列表
搜索
主站蜘蛛池模板: 儋州市| 航空| 玛曲县| 汶上县| 陆良县| 东平县| 苏尼特左旗| 湘乡市| 恩施市| 钦州市| 常熟市| 焉耆| 乌海市| 蓬莱市| 沧州市| 石阡县| 宿松县| 绥江县| 新巴尔虎左旗| 雅安市| 乌拉特后旗| 泌阳县| 连山| 阜南县| 从化市| 玛曲县| 昆明市| 梨树县| 青州市| 五大连池市| 竹溪县| 新建县| 孟津县| 隆昌县| 加查县| 北海市| 竹溪县| 广平县| 龙胜| 乐安县| 绥滨县|