2013-03-18 33 views
0

我已經有了一個來自數據庫的一些單元格的gridview。用戶應該一次只能選擇1列的行。例如:創建一個gridview事件來選擇同一列中的某些元素

0 1 2 
0 a b c  
1 d e f  
2 g h i 

如果用戶拖動鼠標(同時按下左按鈕)從0/0 = a至1/1 = E 僅小區A和d應選擇。 (多列選擇不應該被允許)

如果有人能幫忙,

謝謝

這就是我得到了開頭:

private void myDataGridView1_MouseDown(object sender, MouseEventArgs e) { 

      var hti = dataGridView1.HitTest(e.X, e.Y); 
      coord[0] = hti.RowIndex; 
      coord[1] = hti.ColumnIndex; 
} 

private void dataGridView1_MouseUp(object sender, MouseEventArgs e) 
     { 
      var hti = dataGridView1.HitTest(e.X, e.Y); 
      coord[2] = hti.RowIndex; 
      coord[3] = hti.ColumnIndex; 

      // Some actions 
} 

這個例子還是選擇多列。

+0

好的,你想選擇在同一行中的單元格。但是,您如何確定應選擇哪一列的哪一列。你使用什麼事件? – 2013-03-18 12:24:54

+0

好吧,你的英語對我來說有點神祕,所以你想要什麼。即使用戶選擇了多列,也只選擇一列的元素。 – 2013-03-19 14:40:59

+0

對不起,我的壞話,我有很多問題要說,但可以理解你。 但是,是的。如果用戶開始在列「A」中選擇,他不能選擇多列,只能選擇列「A」中的行。 – Weautus 2013-03-19 16:13:44

回答

0
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace gridview 
{ 
    public partial class Form1 : Form 
    { 
     bool buttonIsPressed = false; 
     int rowIndex, columnIndex; 

     public Form1() 
     { 
      InitializeComponent(); 
      completeDataGridview(); 
     } 

     //this function only creates a filled DataGridView 
     private void completeDataGridview() 
     { 
      dataGridView1.RowCount = 3; 
      for (int x = 0; x < 3; x++) 
      { 
       for (int y = 0; y < 3; y++) 
       { 
        dataGridView1.Rows[x].Cells[y].Value = ((char)(97 + y + 3 * x)).ToString(); 
       } 
      } 

      // this part is stolen from http://stackoverflow.com/questions/7412098/fit-datagridview-size-to-rows-and-columnss-total-size 
      int height = 0; 
      foreach (DataGridViewRow row in dataGridView1.Rows) 
      { 
       height += row.Height; 
      } 
      height += dataGridView1.ColumnHeadersHeight; 

      int width = 0; 
      foreach (DataGridViewColumn col in dataGridView1.Columns) 
      { 
       width += col.Width; 
      } 
      width += dataGridView1.RowHeadersWidth; 

      dataGridView1.ClientSize = new Size(width + 2, height); 

     } 

     private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) 
     { 
      buttonIsPressed = true; 
      rowIndex = e.RowIndex; 
      columnIndex = e.ColumnIndex;    
     } 

     private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) 
     { 
      if (buttonIsPressed) 
      { 
       int startingRow = 0; 
       if (rowIndex > e.RowIndex) 
        startingRow = e.RowIndex; 
       else 
        startingRow = rowIndex; 

       //at first we have to deselect every cell 
       deselectEveryCell(); 

       //then reselect the old ones 
       int amountOfRows = Math.Abs(rowIndex - e.RowIndex) + 1; 
       for (int x = 0; x < amountOfRows; x++) 
       { 
        dataGridView1.Rows[startingRow + x].Cells[columnIndex].Selected = true; 
       } 
      } 
      buttonIsPressed = false; 
     } 

     private void deselectEveryCell() 
     { 
      for (int x = 0; x < dataGridView1.RowCount; x++) 
      { 
       for (int y = 0; y < dataGridView1.ColumnCount; y++) 
       { 
        dataGridView1.Rows[x].Cells[y].Selected = false; 
       } 
      } 
     } 

     private void dataGridView1_MouseLeave(object sender, EventArgs e) 
     { 
      if (buttonIsPressed) 
       deselectEveryCell(); 
     } 
    } 
} 

這是您需要的代碼。 功能completeDataGridview()並不重要。

您需要3個活動。 CellMouseDown,CellMouseUpMouseLeave

CellMouseDown我們得到初始選定單元格的信息並保存在rowindex和columnindex中。並且buttonPressed設置爲true(這可能會在稍後有所幫助)。

如果在鼠標指向某個單元格時釋放鼠標按鈕,則會觸發事件CellMouseUp。我們首先檢查哪個單元靠近DGV的頂部(單元格在開始或當前單元格)。 然後取消選擇每個單元格。 然後我們檢查我們選擇的細胞數量(存儲在amountOfRows中)。 然後我們再次選擇「正確」的單元格。

事件MouseLeave也可能很重要。否則,用戶可以通過開始選擇一個單元並將鼠標拖出DGV來「濫用」你的功能。沒有這個功能,他將能夠選擇多個列。

如果您還有任何問題,請不要猶豫,問問。

相關問題