c# - Create a gridview event who select some elements of the same columns -


i've got gridview cells database. user should able select rows of 1 column @ time. example :

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

if user drags mouse (while pressing left button) 0/0 = 1/1 = e cells , d should selected. (multi column selection shouldn't allowed)

if can help,

thank you

that's got beginning :

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;              // actions  } 

this example still selects multiple columns.

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 creates filled datagridview         private void completedatagridview()         {             datagridview1.rowcount = 3;             (int x = 0; x < 3; x++)             {                 (int y = 0; y < 3; y++)                 {                     datagridview1.rows[x].cells[y].value = ((char)(97 + y + 3 * x)).tostring();                 }             }              // part stolen 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 have deselect every cell                 deselecteverycell();                  //then reselect old ones                 int amountofrows = math.abs(rowindex - e.rowindex) + 1;                 (int x = 0; x < amountofrows; x++)                 {                     datagridview1.rows[startingrow + x].cells[columnindex].selected = true;                 }             }             buttonispressed = false;         }          private void deselecteverycell()         {             (int x = 0; x < datagridview1.rowcount; x++)             {                 (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();         }     } } 

this code need. function completedatagridview() isn't important.

you need 3 events. cellmousedown, cellmouseup , mouseleave.

in cellmousedown info of initial selected cell , save in rowindex , columnindex. , buttonpressed set true (this helpful later on).

if mousebutton released while mousepointer points @ cell event cellmouseup fired. first check cell nearer top of dgv (the cell @ beginning or current one). every cell deselected. afterwards check amount of cells did select (which stored in amountofrows). select "correct" cells again.

the event mouseleave important too. otherwise user can "abuse" ur functionality starting select 1 cell , dragging mouse out of dgv. without function able select multiple columns.

if still got questions don't hesitate ask.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -