Why removed without selected Item from gridview in C# - c#

Please help me why my selected item not deleted from grid-view in c#.
Code:
rowindex = this.AB.ABProducts.Count - 1;
foreach (GridViewRow row in gc.GridRows)
{
CheckBox chkRow = row.Cells[2].FindControl("chkID") as CheckBox;
{
if (rowindex >= 0)
{
if (chkRow.Checked)
{
this.AB.ABProducts.Remove(rowindex);
}
}
}
rowindex--;
}

Related

DataGridView C# windows application(First row not selecting which is checkbox if mouse click on column Header)

Can please any one help me on this. I have developed a c# windows application which has DataGridView first column has checkboxes. if I click on first column header it selects all the row level check boxes except the first row. For selecting all row level check boxes I have an event of dataGridView1_ColumnHeaderMouseClick and the code is:
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
column.SortMode = DataGridViewColumnSortMode.NotSortable;
}
if (e.ColumnIndex == 0)
{
if (chek == 0)
{
try
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
string paymentValue = dataGridView1.Rows[i].Cells[18].Value.ToString();
string incmngp = dataGridView1.Rows[i].Cells[20].Value.ToString();
if (paymentValue == "N" && incmngp =="")
{
dataGridView1.Rows[i].Cells[0].Value = 1;
chek = 1;
}
}
if (chek == 1)
{
btn_update.Text = "Update";
}
}
catch (Exception ) { }
}
else if(chek==1)
{
try
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
dataGridView1.Rows[i].Cells[0].Value = 0;
chek = 0;
}
if (chek == 0)
{
btn_update.Text = "OK";
}
}
catch (Exception) { }
}
}
Note: chek is the variable declared on initialize stage
Set your Selection mode property of data grid view to ColumnHeaderSelect
and make sure all your 'Text' columns have SortMode set to NotSortable
UPDATE 2
In which case, Undo everything I ever told before and do that like this
Before you are assigning any DataTable to dataGridView1.
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
dataGridView1.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
foreach(DataGridViewColumn dc in dataGridView1.Columns)
{
dc.SortMode = DataGridViewColumnSortMode.NotSortable;
}
dataGridView1.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
UPDATE 3
Add an event handler for your dataGridView1's ColumnHeaderMouseClick Event
like below
Add the below code (Generic code if you want to use the same functionality for any column of check boxes)
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
//Enter your own column index here
if(e.ColumnIndex == 0)
foreach(DataGridViewRow row in dataGridView1.Rows)
foreach (DataGridViewCell cell in row.Cells)
{
//Check if the cell type is of a CheckBoxCell
if (cell.GetType() == typeof(DataGridViewCheckBoxCell))
{
DataGridViewCheckBoxCell c = (DataGridViewCheckBoxCell)cell;
c.TrueValue = "T";
c.FalseValue = "F";
if (c.Value == c.FalseValue|| c.Value == null )
c.Value = c.TrueValue;
else
c.Value = c.FalseValue;
}
}
dataGridView1.RefreshEdit();
}
This is a very bizarre bug in Winforms. The problem more generally applies not to the first row, but to the first selected cell in any row of DataGridViewCheckBoxCell(s). You can select the CheckBox cell by clicking on the check box, or select the cell outside the check box, the behavior is the same. If you select 3 check boxes in the middle of your grid, the first of those three will freeze and not update properly. If you try to clear the selection in code, with a dataGridView1.ClearSelection() method call, it still does not work.
The correct answer is to call datagridview1.RefreshEdit() right after you change the checkbox data. You can't just call it after all changes are made. It must be made for each change in the CheckBox value.
foreach (DataGridViewRow row in Results.Rows)
{
var ck = (DataGridViewCheckBoxCell) row.Cells["check"];
ck.Value = ck.TrueValue;
Results.RefreshEdit();
}

how select multiple row using shift and Ctrl key in datagridview C#

My requirement : In datagridview I need select row by clicking the row header and selected row should maintain until I will click the other row header also same time I should select cell too.
My Problem : I can't select multiple row using Shift and Ctrl key.
my code :
List< DataGridViewRow> selectedRows = new List< DataGridViewRow>();
void selectRows()
{
dataGridView1.SuspendLayout();
foreach (DataGridViewRow r in dataGridView1.Rows)
{
r.Selected = selectedRows.Contains(r);
}
dataGridView1.ResumeLayout();
}
private void dataGridView1_RowHeaderMouseClick(object sender,DataGridViewCellMouseEventArgs e)
{
DataGridViewRow clickedRow = dataGridView1.CurrentRow;
if (selectedRows.Contains(clickedRow))
{
selectedRows.Remove(clickedRow);
}
else
{
selectedRows.Add(clickedRow);
}
selectRows();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if ((row.Index != e.RowIndex) && !row.Selected)
{
row.DefaultCellStyle.BackColor = Color.White;
}
else
{
selectedRows.Remove(clickedRow);
row.Selected = true;
row.DefaultCellStyle.BackColor = Color.Blue;
}
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.DefaultCellStyle.BackColor == Color.Blue)
{
row.Selected = true;
}
}
}
You have to set your enable your datagridview's multiselect dataGridView.MultiSelect = true;
Be familiar with using a debugger, you will be able to find out where the issue is.
You are clearing the selection in your loop
foreach (DataGridViewRow row in dataGridView1.Rows)
{
}
Rethink the if-else logic in it and you will see why. You are clearing your previous selections when you are not suppose to.

How do I unselect all selected CheckBoxes in a DataGridView

I want to uncheck all selected CheckBoxes from DataGridView which is generated dynamically?
I tried like following
for (int i = 0; i < MygridView.Rows.Count; i++)
{
DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)MygridView.Rows[i].Cells[0];
if (cell.Value == cell.TrueValue)
{
// (row.Cells[CheckBoxColumn.Index] as DataGridViewCheckBoxCell).value = false;
Need to unchecked all checked checkboxes
}
}
Or Is there any other method?
Try this:
foreach (DataGridViewRow item in MygridView)
{
DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)item.Cells[0];
cell.Value = false;
}

DataGridView- How to jump to the selected row in search?

I use the following code to find a row in DataGridView and highlight the row.
private void btnSearch_Click(object sender, EventArgs e)
{
currentMode = ModeSelection.Search;
if (cmbSearchBy.SelectedIndex == Convert.ToInt16(SearchBy.MaterialID))
{
dgvSearchResults.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
int rowIndex = -1;
try
{
foreach (DataGridViewRow row in dgvSearchResults.Rows)
{
if (row.Cells[1].Value.ToString().Equals(materialLocation.MaterialID))
{
//Select the row here
rowIndex = row.Index;
dgvSearchResults.Rows[rowIndex].Selected = true;
break;
}
}
}
catch (Exception ex) { throw ex; }
}
It works perfectly. Problem is my DataGridView has over 500 records and if the selected row is near the bottom of the DataGridView, users have to scroll all the way down to the bottom. Which code can I use to jump to the row that I am looking for? Any help will be very much appreciated!
I found out that I can use DataGridView.FirstDisplayedScrollingRowIndex Property to scroll to the selected row index and display it as the first row in DataGridView.
This is how I used in my program-
if (row.Cells[1].Value.ToString().Equals(materialLocation.MaterialID))
{
rowIndex = row.Index;
dgvSearchResults.ClearSelection();
dgvSearchResults.Rows[rowIndex].Selected = true;
dgvSearchResults.FirstDisplayedScrollingRowIndex = rowIndex;
dgvSearchResults.Focus();
break;
}
int rowIndex = -1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0].Value.ToString().Equals(searchString))
{
rowIndex = row.Index;
break;
}
}
if (rowIndex >= 0)
{
dataGridView1.CurrentCell = dataGridView1[visibleColumnIndex, rowIndex];
}
visibleColumnIndex - selected cell must be visible

Checkboxlist within gridview cell cannot count all selected items?

I have these checkboxlist each cell of a gridview. Now, I am trying to get the selected items each checkboxlist but it failed. Any help please! Thanks!
foreach (GridViewRow gvRow in gvReg.Rows)
{
for (int ctr = 0; ctr <= 4 - 1; ctr++)
{
if (ctr == 0)
{
szCheckBoxListName = "cblMultiSelect";
szRegionName = "lblRegionName";
}
else
{
szCheckBoxListName = "cblMultiSelect" + ctr;
szRegionName = "lblRegionName" + ctr;
}
cbl=(CheckBoxList)gvRow.Cells[ctr].FindControl(szCheckBoxListName);
if (cbl.Items.Count > 0)
{
foreach (ListItem li in cbl.Items)
{
if (li.Selected)
{
iItemCount = iItemCount + 1;
}
}
}
}
}
itemCount returns zero always even if I have selected several items on those checkboxlists.
Are you data binding on Page_Load method? If yes, you must do this:
if(!IsPostBack)
{
GridView1.DataSource = Your Datas;
}

Categories