Want to check whether a gridview contain duplicate value - c#

I want to check a wether a gridview contain duplicate value on button click event

This will find the duplicate values in dataGridView and add them to a alreadySeen list. Then show message box.
private void button2_Click_1(object sender, EventArgs e)
{
int i = 0,j=0;
string check1 = "",check2="";
List<string> alreadySeen = new List<string>();
for (i=0;i<dataGridView1.Rows.Count-1;i++)
{
check1 = this.dataGridView1.Rows[i].Cells[0].Value.ToString();
j = i;
for (j=j+1; j < dataGridView1.Rows.Count - 1; j++)
{
check2 = this.dataGridView1.Rows[j].Cells[0].Value.ToString();
if (check1==check2)
{
if (!alreadySeen.Contains(check1))
alreadySeen.Add(check1);
}
}
}
//Show duplicate value
foreach (var x in alreadySeen)
{
MessageBox.Show(x);
}
}

Related

how to check if there is a duplicate string in repositorycombo box?

how to check if there is a string already exist in repository
as you can see there's a duplicate dance in column genre. and when i click on auto filter row the dance got read twice. how to discard it there's a same string ?. so there's only one dance in the repositoryitembox
private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
repositoryItemComboBox1.Items.Clear();
for (int i = 0; i < gridView1.RowCount; i++)
{
var genre = gridView1.GetDataRow(i)["genre"].ToString();
//if (genre.Contains("Dance"))
//{
// repositoryItemComboBox1.Items.Add("!!!");
//}
repositoryItemComboBox1.Items.Add(genre);
}
Edit : i dont want to check by string as genre.contains("Dance").
you could simply done that by using Combobox1.Items.Contains function
for (int i = 0; i < gridView1.RowCount; i++)
{
string genre = gridView1.GetDataRow(i)["genre"].ToString();
// Check if the genre already existed or not
if (!comboBox1.Items.Contains(genre))
{
comboBox1.Items.Add(genre);
}
}
You don't check against the string genre, but against the combobox items you already have. Therefore:
private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
repositoryItemComboBox1.Items.Clear();
for (int i = 0; i < gridView1.RowCount; i++)
{
var genre = gridView1.GetDataRow(i)["genre"].ToString();
if(!repositoryItemComboBox1.Items.Contains(genre))
{
repositoryItemComboBox1.Items.Add(genre);
}
}
}

how to get number of items checked in checklist in textbox?

I want to add no items checked in textbox from checklist box. but nothing is showing in textbox.
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < chklst_scrips.Items.Count; i++)
{
if (chklst_scrips.GetItemCheckState(i) == CheckState.Checked)
{
for (int j = 0; ;j++ )
{
textBox1.Text = Convert.ToString(j);
}
}
}
}
GetItemChecked method will be useful in order to find the checked items from the CheckboxList.
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
string str = (string)checkedListBox1.Items[i];
textBox1.Text += str;
}
}
Just create a counter and give it an initial value of 0
int counter = 0;
And then, increment the counter every time a checkbox is checked, as shown here for example if you have a chechbox called checkBox1:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
counter++
//Your code here
}
You will need to increment the counter in all the checkboxes you have
If you are using a checkedListBox called for example checkedListBox1, you can just use checkedListBox1.CheckedItems.Count and get the number of checked items.

Datagrid Column Selected

I want to check one column of DataGridView to see whether if it is 0 or 1. I also want it to print the number of 0 or 1 and write it to the TextBox. I tried this code but I it gives an error.
for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
{
Application.DoEvents();
DataGridViewRow rw = new DataGridViewRow();
if (Convert.ToInt16(dataGridView1.Rows[i].Cells["values4"].Value) == 0)
{
rw.HeaderCell.Value = rw.Index + 1;
txtBasarili.Text = rw.HeaderCell.Value.ToString();
}
In your Grid_Click Event Write then
private void grdform1_CellClick(object sender, DataGridViewCellEventArgs e)
{
If(e.ColumnIndex==0 ||e.ColumnIndex==1)
{
txtshowcolunm.text=e.ColumnIndex;
}
else
{
txtshowcolunm.text=e.ColumnIndex;
}
}

having problems with checklistbox click events

This code works but the results show up when I check and then uncheck again in the first checkbox. I want to check only once.Does anyone have an opinion? Thanks!
private void clb_grup_MouseClick(object sender, MouseEventArgs e)
{
Liste.Items.Clear();
string s = "";
foreach (var item in clb_kisiler.CheckedItems)
{
s = s + item.ToString() + " ";
}
string[] secilenler = s.Split(' ');
if (clb_grup.GetItemChecked(0) == true)
{
for (int x = 0; x < clb_kisiler.Items.Count; x++)
{
clb_kisiler.SetItemChecked(x, true);
}
for (int i = 0; i < clb_kisiler.Items.Count; i++)
{
Liste.Items.Add(clb_kisiler.Items[i].ToString());
}
}
else if (clb_grup.GetItemChecked(1) == true)
{
for (int x = 0; x < clb_idari.Items.Count; x++)
{
clb_idari.SetItemChecked(x, true);
}
for (int i = 0; i < clb_idari.Items.Count; i++)
{
Liste.Items.Add(clb_idari.Items[i].ToString());
}
}
else if (clb_grup.GetItemChecked(2) == true)
{
for (int x = 0; x < clb_teknik.Items.Count; x++)
{
clb_teknik.SetItemChecked(x, true);
}
for (int i = 0; i < clb_teknik.Items.Count; i++)
{
Liste.Items.Add(clb_teknik.Items[i].ToString());
}
}
foreach (object i in clb_kisiler.CheckedItems)
{
Liste.Items.Add(i.ToString());
}
}
NEW CODE
My friend used string arrays instead of the combo boxes.She is also having the same problem as me,and she also tried to write a code that would prevent dublicate items being showed up in the list box.She has one checked list box the same as me,her second checked list box is empty,and one list box.Heres the code.
private void chklstbx_bolum_ItemCheck(object sender, ItemCheckEventArgs e)
{
//event for the first check list box
string[] tumu = { "Jane", "Tammy", "John", "Emily", "Susan", "Julie", "Amelia", "Katherine" };
string[] idari = { "Julie", "Amelia", "Katherine" };
string[] teknik = { "Jane", "Tammy", "John", "Emily", "Susan" };
if (chklstbx_bolum.GetItemChecked(0) == true)
{
//if the first box in the first check list box is checked then do this
//this part of the code works fine but it doesnt check if there are //duplicates of the same name in the list box
chklstbx_sonuc.Items.Clear();
for (int i = 0; i < 5;i++ )
{
chklstbx_sonuc.Items.Add(teknik[i]);
chklstbx_sonuc.SetItemChecked(i, true);
lstbx_sonuc.Items.Add(teknik[i]);
}
}
else if (chklstbx_bolum.GetItemChecked(1) == true){
//do this if the second box in the first check box list is checked
//Here the program checks to see if there are duplicates when adding from the second check list box but the program just freezes.
chklstbx_sonuc.Items.Clear();
int x=0;
do
{
for (int i = 0; i < 3; i++)
{
chklstbx_sonuc.Items.Add(idari[i]);
chklstbx_sonuc.SetItemChecked(i, true);
lstbx_sonuc.Items.Add(idari[i]);
}
} while ( x== 0);
x++;
for (int t = 0; t < lstbx_sonuc.Items.Count; t++)
{
for (int s = 0; s < chklstbx_sonuc.Items.Count; s++)
{
if (chklstbx_sonuc.Items[s].ToString() != lstbx_sonuc.Items[t].ToString())
lstbx_sonuc.Items.Add(chklstbx_sonuc.Items[s]);
}
}
}
}
So now the second question is why does the second else if statement create a problem and makes the program not respond? And we still want to know how are we able to transfer the items in the string array without having to check and uncheck but directly transfer when its checked?
The click event is not the right one. you should use the check event and then verify in the event arguments if the checkbox has been checked or unchecked.
EDIT
I've read your other problem. There is an easier solution than manually checking which has been checked.
You need to assign a value to each checkbox with the ID of the list you want to transfer. You can then check in your code which value has been checked (likely through the sender) and use FindControl(checkboxvalue) to find the list that you want to transfer. Then you first transfer all the items, and then you check them. As I mentioned above, you also need to verify in your checkedeventargs (or whatever the eventargs parameter of the checked event is) if the checkbox is currently checked or unchecked.

how to ensure that user select different comobox option in Datagridview

i have gridview with a combo box column Named ColumnLocationDemo. I want to ensure that user every time selected distinct option from comobox. i am trying this code message box appearing but dont know how to change the index of columnLocationDemo?? there is not selectedIndex property in this gvCombobox
private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
string str1 = dataGridView1.CurrentRow.Cells[1].Value.ToString();
for (int i = 0; i < dataGridView1.Rows.Count - 2; i++)
{
string str = dataGridView1.Rows[i].Cells[1].Value.ToString();
if (str==str1)
{
MessageBox.Show("same occur");
ColumnLocationDemo
}
}
}
How about this...
Don't use the currentRow. Just compare the new value with the values already selected.
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = i + 1; j < dataGridView1.Rows.Count - 1;j++ )
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == dataGridView1.Rows[j].Cells[0].Value.ToString())
{
dataGridView1.Rows[j].Cells[0].Value = "";
}
}
}
columnLocationDemo.SelectedIndex = 0;
If that is the name of your comboBox. Also you can select any index.
Try this:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//your work;
}

Categories