C# DataGridView

來源:互聯網
上載者:User

標籤:ret   pos   rip   event   mes   data   visible   sele   ons   

 

   private void InitDataGridView()        {            // Create an unbound DataGridView by declaring a column count.            dataGridView1.ColumnCount = 3;            dataGridView1.ColumnHeadersVisible = true;            dataGridView1.AllowUserToAddRows = false;            dataGridView1.AllowUserToDeleteRows = false;            dataGridView1.AllowUserToOrderColumns = false;                  dataGridView1.MultiSelect = false;            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;            dataGridView1.ReadOnly = true;            // Set the column header style.            DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();            columnHeaderStyle.BackColor = Color.Beige;            columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Regular);            dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;            // Set the column header names.            dataGridView1.Columns[0].Name = "C1";            dataGridView1.Columns[1].Name = "C2";            dataGridView1.Columns[2].Name = "C3";                          }        private void AddDataGridViewData()        {            for (int i = 0; i < 3; i++)            {                int index = this.dataGridView1.Rows.Add();                this.dataGridView1.Rows[index].Cells[0].Value = i.ToString() + "1";                this.dataGridView1.Rows[index].Cells[1].Value = i.ToString() + "2";                this.dataGridView1.Rows[index].Cells[2].Value = i.ToString() + "3";            }                   }        private string GetSelectedValue()        {            int index= dataGridView1.CurrentRow.Index;            return dataGridView1.Rows[index].Cells["C2"].Value.ToString();                               }        //右鍵顯示菜單        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)        {            if (e.Button == MouseButtons.Right)            {                if (e.RowIndex >= 0)                {                    //若行已是選中狀態就不再進行設定                    if (dataGridView1.Rows[e.RowIndex].Selected == false)                    {                        dataGridView1.ClearSelection();                        dataGridView1.Rows[e.RowIndex].Selected = true;                    }                    //只選中一行時設定作用儲存格                    if (dataGridView1.SelectedRows.Count == 1)                    {                        dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];                    }                    //彈出操作菜單                    contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);                }            }        }

 

 //清除DataGridView資料        private void DataGridViewClear()        {                        for (int i = 0; i < dataGridView1.RowCount; i++)            {                dataGridView1.Rows.Remove(dataGridView1.Rows[i]);                i--;            }        }

 

C# DataGridView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.