Click here to Skip to main content
14,689,406 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a datagridview. i want to select some cells which spreaded in different columns and rows. when i hit Delete key this cells should be clear. i could achieve this for cells of text type as follows (I use MS Access as the database and all fields are numbers)
if (e.KeyData == Keys.Delete)
                   {

                       foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
                       {
                           // if keep column in text type in MS access
                           string i = "";
                           cell.Value = (object)i;


                       }



this really clear all deleted cells. but what shall i do if the cells are numbers . how i can assign null value to these numbers while deleting.

if (e.KeyData == Keys.Delete)
                    {

                        foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
                        {
                            // if keep column in text type in MS access
                            int i =0;//here i want some null value
                            cell.Value = (object)i;

                            
                        }



the above code make all deleted cell with zeros . I want to avoid that situation. I dont want to place a zero for null.

another approach I did is

if (e.KeyData == Keys.Delete)
                    {

                        foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
                        {
                            
                            cell.Value = cell.DefaultNewRowValue;

                            
                        }

but this do not work when i select cells of more than one row . how i can solve it;

please somebody help please......
Posted
Updated 15-Jun-20 8:08am
v2

Let cell value as string
exmple:
int i=2
cell.value= (i).tostring()

i think you may use cell as string a use parsing
to read value ....
If(cell.value.tosting().trim()!="")
Int i = int.Parse(cell.value.tostring())
   
Comments
[no name] 12-Oct-11 10:26am
   
for more clarification dear friend, i am having a MS Access database its columns I set as numbers.the database is retrieved to the datagrid view. so when I select some cells and delete it should be empty . ie null . this is happening when I make database columns as text.by using the code


string i = ""; cell.Value = (object)i; but for numbers what should I do to get the same behaviour
[no name] 12-Oct-11 10:27am
   
I just want to replace the deleted cells with nothing
SIMPLY try cell.value = null;
must work
   
Comments
[no name] 11-Oct-11 10:43am
   
the proposed cell are used to show integers . so it can't be
initialised with null . in case of string it will work
Try this for a specific cell (VB);

Datagrid.Rows(RowIndex).Cells(ColIndex).Value = DBNull.Value
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900