i am using checkbox in gridview..to get the checkbox id i am using the following code..
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkDelete = (CheckBox)GridView1.Rows.Cells[0].FindControl("chkSelect");
if (chkDelete != null)
{
if (chkDelete.Checked)
{
strID = GridView1.Rows.Cells[1].Text;
idCollection.Add(strID);
}
}
}
BUT THE KEYWORD "CELLS"..do not support..i am getting an error.."System.Web.UI.WebControls.GridViewRowCollection' does not contain a definition for 'Cells' "
This is the way you have to check
foreach (GridViewRow grRow in grdACH.Rows)
{
CheckBox chkItem = (CheckBox)grRow.FindControl("checkRec");
if (chkItem.Checked)
{
strID = ((Label)grRow.FindControl("lblBankType")).Text.ToString();
}
}
That's correct; the GridViewRowCollection class does not contain either a method or a property with the name Cells. The reason that matters is that the Rows property of the GridView control returns a GridViewRowCollection object, and when you call GridView1.Rows.Cells, it is searching for a Cells property on the GridViewRowCollection object returned by the Row property.
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkDelete = (CheckBox)GridView1.Rows[i].FindControl("chkSelect");
if (chkDelete != null)
{
if (chkDelete.Checked)
{
strID = GridView1.Rows[i].Cells[1].Text;
idCollection.Add(strID);
}
}
}
foreach (GridViewRow rowitem in GridView1.Rows)
{
CheckBox chkDelete = (CheckBox)rowitem.Cells[0].FindControl("chkSelect");
if (chkDelete != null)
{
if (chkDelete.Checked)
{
strID = rowitem.Cells[1].Text;
idCollection.Add(strID);
}
}
}
Related
I have a gridview control on my form.Its first column is checkboxcolumn and the other one textboxcolumn.I am populating Textbox columns with some string values coming from list
like this way
for (int i = 0; i < listeList.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[1].Value = listeList[i];
}
What am i trying to do is to select all the checboxes in the gridview once the user click button How can I do this
Here is what I have tried
private void btnselectAll_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
// row.Cells[0].Value = "true";
//if (row.Cells[0].Value != ull)
//{
// if (Convert.ToBoolean(row.Cells[0].Value))
// {
// MessageBox.Show(row.Cells[1].Value.ToString());
// }
//}
}
}
Cast the appropriate cell to DataGridViewCheckBoxCell:
for(int row = 0; row < this.dataGridView1.Rows.Count; row++)
{
var chkCell = dataGridView1[0, row] as DataGridViewCheckBoxCell;
// read:
bool isChecked = (bool)chkCell.EditedFormattedValue;
// assign:
chkCell.Value = true;
}
I have a loaded DataGrid. In that DataGrid the first column is a CheckBox, and the second column is "Name". Also I have saved one field "Name" in the database . Here I want to make the CheckBox to be checked if the Name is equal to the data which I stored in the database.
Here my problem is that ,I am getting only one CheckBox to to be checked.
ex:[ If my expected result is 1st, 2nd and 3dr CheckBoxes to be checked , But I am getting only the 3rd one as checked. ]
my sample code is
foreach (GridViewRow row in GrdProduct.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Label lblproduct = (Label)row.FindControl("lblProduct");
CheckBox chkSelect = (CheckBox)row.FindControl("chkSelectAll");
for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
{
DataRow r = dt.Rows[rowIndex];
if (Convert.ToString(r["productName"]) == lblproduct.Text)
{
chkSelect.Checked = true;
}
else
{
chkSelect.Checked = false;
}
}
}
Finally I got the solution ... Simply removed the else part
else
{
chkSelect.Checked = false; }
--
thank you all...
Sorry I am blind. You are looping through the datarows and of course you most likely have only one match. In all other cases it becomes unchecked right after. Use the following:
foreach (GridViewRow row in GrdProduct.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Label lblproduct = (Label)row.FindControl("lblProduct");
CheckBox chkSelect = (CheckBox)row.FindControl("chkSelectAll");
chkSelect.Checked = false;
for (int rowIndex = 0; rowIndex < dt.Rows.Count || !chkSelect.Checked; rowIndex++)
{
DataRow r = dt.Rows[rowIndex];
if (Convert.ToString(r["productName"]) == lblproduct.Text)
{
chkSelect.Checked = true;
}
}
}
Als when comparing strings I recommend http://msdn.microsoft.com/en-us/library/system.string.equals(v=vs.110).aspx
COmpared what you are going to do with the data and controls in the grid I also recommend additional validation.
I am trying to search every cell in my datagridview for a value "test". However it is only searching the first row... (i believe it is searching all the columns) Any ideas on how I can fix this?
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
string searchValue = "test";
int searching = -1;
while (searching < 7)
{
searching++;
try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[searching].Value.ToString().Equals(searchValue))
{
row.Cells[searching].Selected = true;
break;
}
}
}
catch (Exception exc)
{
// MessageBox.Show(exc.Message);
}
}
use this snippet.. basically we iterate through every row/column and set its value as selected if we find a match.
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
string searchValue = "test";
for (int row = 0; row < dataGridView1.Rows.Count; ++row)
{
for (int col = 0; col < dataGridView1.Columns.Count; ++col)
{
var cellValue = dataGridView1.Rows[row].Cells[col].Value;
if (cellValue != null && cellValue.ToString().Equals(searchValue))
{
dataGridView1.Rows[row].Cells[col].Selected = true;
// if you want to search every cell for the searchValue then you shouldn't break.
// break;
}
}
}
you can also do the above as follows, using concise LINQ code:
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
string searchValue = "test";
dataGridView1.Rows.ToList().ForEach(row => row.Cells.ToList().ForEach(cell =>
{
cell.Selected = (cell.Value != null && cell.Value.ToString().Equals(searchValue));
}));
I am currently using this code:
foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
dataGridView1.Rows.RemoveAt(item.Index);
}
I have checkmarks down the first column, but with this code, it only get the selected. How do i get the Selected CheckBoxes only to delete with the row?
You want something like
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (Convert.ToBoolean(dataGridView1.Rows[i]
.Cells[yourCheckBoxColIndex].Value) == true)
{
dataGridView1.Rows.RemoveAt(i);
}
}
I hope this helps.
Try something like this:
foreach(DataGridViewRow row in this.dataGridView1.Rows)
{
var checked = Convert.ToBoolean(row.Cells[0].Value); // Assuming the first column contains the Checkbox
if(checked)
dataGridView1.Rows.RemoveAt(row.Index);
}
It could be something like this... This is an example for listview, however the concept is almost there. Loop through the item and find the checkbox id and remove those selected. Hope this helps.
public void btnDeleteClick(object sender, EventArgs e)
{
// Iterate through the ListViewItem
foreach (ListViewItem row in ListView1.Items)
{
// Access the CheckBox
CheckBox cb = (CheckBox)row.FindControl("cbxID");
if (cb != null && cb.Checked)
{
// ListView1.DataKeys[item.DisplayIndex].Values[0].ToString()
try
{
}
catch (Exception err)
{
}
}
}
}
Try this:
if (dgv.SelectedRows.Count>0)
{
dgv.Rows.RemoveAt(dgv.CurrentRow.Index);
}
int s = 1;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = s - 1; j < dataGridView1.Rows.Count - 1; j++)
{
if (Convert.ToInt32(dataGridView1["Stok", j].Value) < 5)
{ s = j; dataGridView1.Rows.RemoveAt(j); break; }
}
}
dataGridView1.Refresh();
I have been saving into the ComboBox a value out of the selected column in datagridview with below code.
My question is:How can I prevent duplicate records when I save the values into the ComboBox? How can I do that?
Code:
int ColumnIndex = dgUretimListesi.CurrentCell.ColumnIndex;
CmbAra.Text = "";
for (int i = 0; i < dgUretimListesi.Rows.Count; i++)
{
CmbAra.Items.Add(dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString());
}
Please try this
private void dgvServerList_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex == 1)
{
string id = dgvServerList[e.ColumnIndex, e.RowIndex].Value.ToString();
int duplicaterow = 0;
for (int row = 0; row < dgvServerList.Rows.Count; row++)
{
if (row != e.RowIndex && id == dgvServerList[e.ColumnIndex, row].Value.ToString())
{
duplicaterow = row + 1;
MessageBox.Show("Duplicate found in the row: " + duplicaterow);
this.dgvServerList[e.ColumnIndex, e.RowIndex].Value = "";
break;
}
}
}
}
catch
{
}
}
you could first transfer your datagridview items to a dictionary (which guarantees uniqueness) and then transfer that dictionary content to the combobox. or you could check for uniqueness yourself using a 'Contains' method on the combobox. you could even tie the dictionary to the combobox as a source for the combobox items.
Dictionary<string,bool> d = new Dictionary<string,bool>();
int ColumnIndex = dgUretimListesi.CurrentCell.ColumnIndex;
CmbAra.Text = "";
for (int i = 0; i < dgUretimListesi.Rows.Count; i++)
{
d[dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString()] = true;
}
CmbAra.Items.AddRange(d.Keys);
Use a set:
int ColumnIndex = dgUretimListesi.CurrentCell.ColumnIndex;
CmbAra.Text = "";
HashSet<string> set = new HashSet<string>();
for (int i = 0; i < dgUretimListesi.Rows.Count; i++)
{
string s = dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString();
if(!set.Contains(s)) {
CmbAra.Items.Add(s);
set.Add(s);
}
}
by using the following check and then determine to add or not
if(CmbAra.Items.Contains(dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString()))
You can use the following code part.
if(!(CmbAra.Items.Contains(dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString())))
{
CmbAra.Items.Add(dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString());
}
else
{
MessageBox.Show("Value Already exists , not added");
}