How to tick all checkboxes - c#

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

Related

convert listbox items to checked rows in datagridview checkbox column

i want to make datagrid (checkecbox column)rows ticked.those rows in datagrid that match studentcode colum already in listbox items .
i have tried the code below.but i don't get the proper result
private void btnConvertItemsToCheckedRows_Click(object sender, EventArgs e)
{
if( ListBox1.ListBox.Items.Count>0)
{
for (int i = 0; i <ListBox1.ListBox.Items.Count; i++)
{
foreach (DataGridViewRow row in GridStudents.DataGridView.Rows)
{
if(row.Cells["StudentCode"].Value.ToString().Equals(ListBox1.ListBox.Items[i]))
{
GridStudents.DataGridView.Rows[i].DataGridView["ChekboxColumn", i].Value = true;
}
}
}
}
}
Try this instead :
private void btnConvertItemsToCheckedRows_Click(object sender, EventArgs e)
{
if( ListBox1.ListBox.Items.Count>0)
{
for (int i = 0; i <ListBox1.ListBox.Items.Count; i++)
{
foreach (DataGridViewRow row in GridStudents.DataGridView.Rows)
{
if(row.Cells["StudentCode"].Value.ToString().Equals(ListBox1.ListBox.Items[i]))
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells["ChekboxColumn"];
chk.TrueValue = true;
chk.Value = chk.TrueValue;
}
}
}
GridStudents.EndEdit();
}
}

How to add a value at the first empty cell in column?

In my application, I have a datagridview. For each column, there is a button. If clicked, I take the column index and now want to add a user enterd value at the first (bottom or top doesn't matter) free cell of that column. If there is no empty cell, I want to create one.
I tried looping through all rows, getting the cell values and checking if they are empty.
string data = "%VALUE%";
int rowIndex = -1;
foreach (DataGridViewRow row in dataGridViewTasks.Rows)
{
data = (string) row.Cells[columnIndex].Value;
if (string.IsNullOrWhiteSpace(data))
{
rowIndex = row.Index;
break;
}
else if (row.Cells[columnIndex].RowIndex == dataGridViewTasks.Rows.Count - 1)
{
rowIndex = dataGridViewTasks.Rows.Add();
break;
}
}
if (string.IsNullOrWhiteSpace(data) && rowIndex > -1)
{
dataGridViewTasks[columnIndex, rowIndex].Value = task.name;
}
Problem with this solution is:
If for example row 1 and 2 of column A are filled, a new value in column B is added in row 3. So that is not what I want.
If I understand you correctly, it`s should work.
Try it:
private void Button1_Click(object sender, EventArgs e)
{
var columnIndex = 1;
var dataGrid = new DataGridView();
var index = GetFirstEmptyCellByColumnIndex(dataGrid, columnIndex);
dataGrid[columnIndex, index].Value = "new value";
}
private int GetFirstEmptyCellByColumnIndex(DataGridView dataGrid, int columnIndex)
{
var index = -1;
foreach (DataGridViewRow row in dataGrid.Rows)
{
if (string.IsNullOrEmpty((string)row.Cells[columnIndex].Value))
{
index = row.Index;
break;
}
}
return index != -1
? index
: dataGrid.Rows.Add();
}

datagridview only searching first row

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));
}));

Prevent duplicate records when I save the values into the ComboBox - C#

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");
}

How do I move items from one gridview to another gridview?

I have two gridviews, and when the user highlights a row on the first gridview and clicks a button, it should move to the second gridview.
When I click on the button that record gets added but it only add the last row I've selected (if I select 20 rows, only the last gets added).
All records that are selected should be moved.
How do I do this in ASP.NET?
private void button1_Click_1(object sender, EventArgs e)
{
DataGridViewRow dr = dataGridView1.SelectedRows[0];
dtItems.Columns.Add("city_ID");
dtItems.Columns.Add("city_Name");
dtItems.Columns.Add("status");
dtItems.Columns.Add("date");
if (dataGridView1.Rows.Count > 1)
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value != null)
{
DataRow row;
row = dtItems.NewRow();
row["city_ID"] = dataGridView1.Rows[i].Cells[1].Value.ToString();
row["city_Name"] = dataGridView1.Rows[i].Cells[2].Value.ToString();
row["status"] = dataGridView1.Rows[i].Cells[3].Value.ToString();
row["date"] = dataGridView1.Rows[i].Cells[4].Value.ToString();
dtItems.Rows.Add(row);
}
}
}
Form2 frm = new Form2(dtItems);
frm.ShowDialog();
}
In Form2 copy this code:
public Form2(DataTable dtIt)
{
dtItems = dtIt;
InitializeComponent();
}
private void AddEmptyRows()
{
for (int i = 1; i <= 5; i++)
{
dataGV.Rows.Add();
}
}
private void Form2_Load(object sender, EventArgs e)
{
AddEmptyRows();
for (int i = 0; i < dtItems.Rows.Count; i++) {
dataGV.Rows[i].Cells[0].Value = dtItems.Rows[i]["city_ID"];
dataGV.Rows[i].Cells[1].Value = dtItems.Rows[i]["city_Name"];
dataGV.Rows[i].Cells[2].Value = dtItems.Rows[i]["status"];
dataGV.Rows[i].Cells[3].Value = dtItems.Rows[i]["date"];
}
dataGV.Enabled = true;
}

Categories

Resources