Copy multiple selected rows from one grid to another - c#

I have this code that copy selected rows from one grid to another
private void btnAddEmployee_Click(object sender, EventArgs e)
{
LayoutControl lc = new LayoutControl();
lc.Dock = DockStyle.Top;
LookUpEdit userShift = new LookUpEdit();
userShift.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
userShift.Properties.DataSource = paint.GetShiftTime();
userShift.Properties.DisplayMember = "ShiftTime";
userShift.Properties.ValueMember = "id";
userShift.Properties.ShowHeader = false;
var date = DateTime.Now;
if (8 < date.Hour && date.Hour < 16)
{
userShift.EditValue = 1;
}
else if (16 < date.Hour && date.Hour < 24)
{
userShift.EditValue = 2;
}
else
{
userShift.EditValue = 3;
}
lc.AddItem(Resources.workingHours, userShift).TextVisible = true;
lc.Height = 50;
this.Controls.Add(lc);
this.Dock = DockStyle.Top;
int[] selectedRows = gridView4.GetSelectedRows();
for(int n=0;n< selectedRows.Length;n++)
//foreach (int index in selectedRows)
{
if (DevExpress.XtraEditors.XtraDialog.Show(lc, Resources.options, MessageBoxButtons.OKCancel) == DialogResult.OK)
{
//Prevent duplicate data
for (int i = 0; i < gridView5.RowCount; i++)
{
if (gridView4.GetRowCellValue(gridView4.FocusedRowHandle, "Matricule").ToString() == gridView5.GetRowCellValue(i, "Matricule").ToString())
{
XtraMessageBox.Show(Resources.employeeAlreadyAdded, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
}
DataRow r = EmplDT.NewRow();
r[0] = gridView4.GetRowCellValue(gridView4.FocusedRowHandle, "Matricule").ToString();
r[1] = gridView4.GetRowCellValue(gridView4.FocusedRowHandle, "Employé").ToString();
r[2] = userShift.Text;
r[3] = userShift.EditValue;
r[4] = txtDate.EditValue;
EmplDT.Rows.Add(r);
this is my code to create Columns in gridview 5
DataTable EmplDT = new DataTable();
void CreateEmployeeTable()
{
EmplDT.Columns.Add("Matricule");
EmplDT.Columns.Add("Employé");
EmplDT.Columns.Add("Heure");
EmplDT.Columns.Add("idShiftTime", typeof(Int32));
EmplDT.Columns.Add("Date", typeof(DateTime));
gridControl5.DataSource = EmplDT;
gridView5.Columns["idShiftTime"].Visible = false;
gridView5.Columns["Date"].Visible = false;
}
i have two problems in this code:
the first one is when i run the code it only add the first record and then i get error message of duplicate .
the second one i want to show layout control only the first time.
thanks in advance and sorry for my english.

Looping thru selected rows from a devexpress gridview and getting a row can be much simpler like this
int[] selectedRows = gridView4.GetSelectedRows();
for (int i = 0; i < selectedRows.Length; i++)
{
// Get a DataRow and fill it with all values from the this selected row
// This is where you went wrong, you kept using only the first selected row
DataRow rowGridView4 = (gridView4.GetRow(selectedRows[i]) as DataRowView).Row;
// Do a check for doubles here
DataRow[] doubles = EmplDT.Select("Matricule = '" + rowGridView4[0].ToString() +"'");
if (doubles.Length > 0)
{
XtraMessageBox.Show(Resources.employeeAlreadyAdded, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// fix for error "This row already belongs to another table"
DataRox row = EmplDT.NewRow();
row[0] = rowGridView4[0];
row[1] = rowGridView4[1];
row[2] = userShift.Text;
row[3] = userShift.EditValue;
row[4] = txtDate.EditValue;
EmplDT.Rows.Add(row);
}
Please note that with testing for doubles at this place will cause all records to be copied until a duplicate is found. So after your error message there might be some records copied and some not.
Is that how you intended this ?
I would leave out the error message and just skip duplicate records. You can still show a message with howmany records where copied if you want.
int[] selectedRows = gridView4.GetSelectedRows();
for (int i = 0; i < selectedRows.Length; i++)
{
// Get a DataRow and fill it with all values from the this selected row
// This is where you went wrong, you kept using only the first selected row
DataRow rowGridView4 = (gridView4.GetRow(selectedRows[i]) as DataRowView).Row;
// Do a check for doubles here
DataRow[] doubles = EmplDT.Select("Matricule = '" + rowGridView4[0].ToString() + "'");
if (doubles.Length == 0)
{
// fix for error "This row already belongs to another table"
DataRox row = EmplDT.NewRow();
row[0] = rowGridView4[0];
row[1] = rowGridView4[1];
row[2] = userShift.Text;
row[3] = userShift.EditValue;
row[4] = txtDate.EditValue;
EmplDT.Rows.Add(row);
}
}

Related

Excel treating date column as general but I need it to treat it as Date when exporting to Excel using C#

I am using Visual Studio and writing code in C# to read an excel then format the excel worksheet to what i want then export it back into an excel. After exporting to Excel, when I open that file, it treats the date column as "General" unless I right click on it and set its type to "Date". I need excel to read it as "Date." The date is formatted correctly but Excel not reading it as a Date. Any suggestions on how to fix this would be greatly appreciated. It's alot of code so I hope this is enough for someone to understand what I am trying to do.
// Excel rows are cell arrays so loop through once to determine
// the actual table geometry
int firstRow = xlSheet.FirstRowNum;
int lastRow = xlSheet.LastRowNum;
int firstCol = xlSheet.GetRow(firstRow).FirstCellNum;
int lastCol = xlSheet.GetRow(lastRow).LastCellNum;
for (int rowIdx = firstRow; rowIdx < lastRow; rowIdx++)
{
var r = xlSheet.GetRow(rowIdx);
if (r == null) { continue; } // Nothing here, move on to the next record
int thisFirstCol = xlSheet.GetRow(rowIdx).FirstCellNum;
int thisLastCol = xlSheet.GetRow(rowIdx).LastCellNum;
firstCol = Math.Min(thisFirstCol, firstCol);
lastCol = Math.Max(thisLastCol, lastCol);
}
// System.Diagnostics.Debug.WriteLine($"First Row/Col:{firstRow} / {firstCol} Last Row/Col {lastRow} / {lastCol}");
// Set up the column headers
for (int colIdx = firstCol; colIdx < lastCol; colIdx++)
{
string colName = $"field{colIdx}"; //default
if (useHeader)
{
try
{
// Only if firstRow isn't the header, otherwise you should choose useHeader = false
// when calling this from the adapter.
// Duplicate column name is rare, but it happens.
colName = xlSheet.GetRow(firstRow).GetCell(colIdx).ToString().Trim();
if (String.IsNullOrEmpty(colName) || dt.Columns.Contains(colName))
{
colName = $"field{colIdx}";
}
}
catch { } // May not have one, so the default will be used
}
dt.Columns.Add(colName, typeof(string));
}
// Now loop again to populate the data
DataRow dr = null;
if (useHeader) {
firstRow++;
lastRow++;
}
for (int rowIdx = firstRow; rowIdx < lastRow; rowIdx++)
{
var xlRow = xlSheet.GetRow(rowIdx);
if (xlRow == null) { continue; } // Nothing here, move on to the next record
dr = dt.NewRow();
// We have to account for a bizarre -1 first column index on some Excel files, Gordon is an example
// Datacolumns always start at 0, so we'll increment it independently, the count should be the same
// in theory...
int dtColIdx = -1;
for (int colIdx = firstCol; colIdx < lastCol; colIdx++)
{
dtColIdx++;
try
{
// Convert the value to a reasonable format
ICell cell = xlRow.GetCell(colIdx);
if (cell != null)
{
switch (cell.CellType)
{
case CellType.Formula:
dr[dtColIdx] = cell.NumericCellValue.ToString();
break;
case CellType.Numeric:
if (DateUtil.IsCellDateFormatted(cell))
{
// TODO: DateTime vs Date vs Time check
dr[dtColIdx] = cell.DateCellValue.ToString("MM/dd/yyyy");
// string xx = dr[dtColIdx].ToString();
// xlSheet.GetColumnStyle(dtColIdx).DataFormat = "mm/dd/yyyy";
}
else
{
dr[dtColIdx] = cell.NumericCellValue.ToString();
}
break;
default:
dr[dtColIdx] = cell.ToString();
break;
}
}
}
catch
{
// May be nothing at this address, plug it with a blank
dr[dtColIdx] = String.Empty;
}
}
dt.Rows.Add(dr);
}
dt.AcceptChanges();
//GenFieldParseCode(dt);
return dt;
}

Updating dataGridView value from another form

hello experts help please, i have two form in my project and one of those form1 have dataGridView and i want to update the gridview according to text value from form 2,
i had done updating dgv but all row updated on button click event
ex. i have 4 rows different description and cell value after click event
all those 4 rows updating to 1 value.
Form 1
dgv cellmouseclick event
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView2.Rows[e.RowIndex];
_choosenPart = row.Cells[0].Value.ToString();
_choosenQty = row.Cells[2].Value.ToString();
_choosenPrice = row.Cells[4].Value.ToString();
_choosenAmount = row.Cells[5].Value.ToString();
_choosenTotal = row.Cells[7].Value.ToString();
}
FrM_Edit EditGV = new FrM_Edit(this);
EditGV.Show();
In form 2
//constructor
Form F2_main = null;
public FrM_Edit(Form DgVForm1)
{
F2_main=DgVForm1;
InitializeComponent();
}
button click event
r2main_choosenPart = textBox1.Text;
r2main_choosenQty = textBox2.Text;
r2main_choosenPrice = textBox3.Text;
r2main_choosenAmount = textBox4.Text;
r2main_choosenTotal = textBox5.Text;
DataGridView Main_dg = (DataGridView)F2_main.Controls["dataGridView2"];
for (int i = 0; i < Main_dg.Rows.Count; i++)
{
Main_dg.Rows[i].Cells[0].Value = r2main_choosenPart;
Main_dg.Rows[i].Cells[2].Value = r2main_choosenQty;
Main_dg.Rows[i].Cells[4].Value = r2main_choosenPrice;
Main_dg.Rows[i].Cells[5].Value = r2main_choosenAmount;
Main_dg.Rows[i].Cells[7].Value = r2main_choosenTotal;
}
this.Close();
//this for loop takes all rows of your DataGridView. And it is normal that you see that all rows are changing because for all rows, you apply the same code.
DataGridView Main_dg = (DataGridView)F2_main.Controls["dataGridView2"];
for (int i = 0; i < Main_dg.Rows.Count; i++)
{
Main_dg.Rows[i].Cells[0].Value = r2main_choosenPart;
Main_dg.Rows[i].Cells[2].Value = r2main_choosenQty;
Main_dg.Rows[i].Cells[4].Value = r2main_choosenPrice;
Main_dg.Rows[i].Cells[5].Value = r2main_choosenAmount;
Main_dg.Rows[i].Cells[7].Value = r2main_choosenTotal;
}
Instead, you should update only the row that you want to updaate.
You can use differents methods.
For exemple:
for (int i = 0; i < Main_dg.Rows.Count; i++)
{
if( Main_dg.Rows[i].Cells[0].Value == "valueToChange") //Checking an ID or other value.
{
Main_dg.Rows[i].Cells[0].Value = r2main_choosenPart;
Main_dg.Rows[i].Cells[2].Value = r2main_choosenQty;
Main_dg.Rows[i].Cells[4].Value = r2main_choosenPrice;
Main_dg.Rows[i].Cells[5].Value = r2main_choosenAmount;
Main_dg.Rows[i].Cells[7].Value = r2main_choosenTotal;
break; // To break after finding the row that you are looking for.
}
}
Or
for (int i = 0; i < Main_dg.Rows.Count; i++)
{
if(i == 2) //to update the third row of the grid.
{
Main_dg.Rows[i].Cells[0].Value = r2main_choosenPart;
Main_dg.Rows[i].Cells[2].Value = r2main_choosenQty;
Main_dg.Rows[i].Cells[4].Value = r2main_choosenPrice;
Main_dg.Rows[i].Cells[5].Value = r2main_choosenAmount;
Main_dg.Rows[i].Cells[7].Value = r2main_choosenTotal;
break; // To break after finding the row that you are looking for.
}
}
Or you can use your DataGridViews selected row to update.

How to scrape datatable from a website with selenium c#?

I am trying to scrape data from the webpage. However, I am having a trouble scraping all of data in the table. I need to switch pages to get all the data and I am willing to get an output with DataGridTable. I am having a trouble figuring out how to do this even though there is a change with number of pages they have in the website. I would like to add information automatically on a data grid table pages by pages. My input(Website) is only showing 25 items. Thats why I have 25 items in DataGridTable. I would like to justify a "number of pages" from "go to end page button"'s element. So that my program knows how many pages are there to scrape from the website. but, if there's a different way, I wanna know thank you.
This is my code for now.
DataTable dt = new DataTable();
var header = driver.FindElement(By.CssSelector("#gridComponent > div.k-grid-header"));
foreach (var row in header.FindElements(By.TagName("tr")))
{
//Configure Number of Col and row
int cellIndex = 0;
string[] arr = new string[32];
//Get Cell Data
foreach (var cell in row.FindElements(By.TagName("th")))
{
// Check the header cell for a checkbox child. If no
// such child exists, add the column.
var headerCheckboxes = cell.FindElements(By.CssSelector("input[type='checkbox']"));
if (headerCheckboxes.Count == 0)
{
//Number of Col Data Load
if (cellIndex <= 29)
{
arr[cellIndex] = cell.Text;
dt.Columns.Add(cell.Text);
}
else
cellIndex++;
}
}
Console.WriteLine(arr);
}
var table = driver.FindElement(By.CssSelector("#gridComponent"));
//Get Row value
foreach (var row in table.FindElements(By.TagName("tr")))
{
//Configure Number of Col and row
int cellIndex = 0;
// Use a list instead of an array
List<string> arr = new List<string>();
//Get Cell Data
foreach (var cell in row.FindElements(By.TagName("td")))
{
// Skip the first column in the row by checking
// if the cell index is 0.
if (cellIndex != 0)
{
string cellValue = "";
Console.WriteLine(cell);
var checkboxes = cell.FindElements(By.CssSelector("input[type='checkbox']"));
if (checkboxes.Count > 0)
{
bool isChecked = false;
isChecked = checkboxes[0].Selected;
cellValue = isChecked.ToString();
}
else
{
cellValue = cell.Text;
}
arr.Add(cellValue);
}
cellIndex++;
}
dt.Rows.Add(arr.ToArray());
}
dataGridView1.DataSource = dt;
driver.FindElement(By.CssSelector("#gridComponent > div.k-pager-wrap.k-grid-pager.k-widget.k-floatwrap > ul > li:nth-child(3)")).Click();
}
This is the table that I am trying to scrape from.
This is the code for the following element that is shown picture above.
<span class="k-icon k-i-arrow-end-right"></span>
Thank you so much.
You may want to consider the index information "1 - 25 out of 64 items", since it is a good indicator of the total number of pages.
Batch = 1 - 25 i.e. 25 items per page
Total items = 64
No. of pages = roundup (64 / 25)
PS: A better option, without any computations, maybe to get the "data-page" attribute of the last page button.
I Finally got the answer for this.
private List<List<string>> GetRecords(IWebElement table)
{
List<List<string>> rows = new List<List<string>>(); ;
//Get Row value
foreach (var row in table.FindElements(By.TagName("tr")))
{
//Configure Number of Col and row
int cellIndex = 0;
// Use a list instead of an array
List<string> cols = new List<string>();
//Get Cell Data
foreach (var cell in row.FindElements(By.TagName("td")))
{
// Skip the first column in the row by checking
// if the cell index is 0.
if (cellIndex != 0)
{
string cellValue = "";
Console.WriteLine(cell);
var checkboxes = cell.FindElements(By.CssSelector("input[type='checkbox']"));
if (checkboxes.Count > 0)
{
bool isChecked = false;
isChecked = checkboxes[0].Selected;
cellValue = isChecked.ToString();
}
else
{
cellValue = cell.Text;
}
cols.Add(cellValue);
}
cellIndex++;
}
rows.Add(cols);
}
return rows;
}
private void button1_Click(object sender, EventArgs e)
{
//Configure to Hide CMD
var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
//Configure to Hide Chrome
ChromeOptions option = new ChromeOptions();
option.AddArgument("--headless");
//HIDING CHROME UN-COMMNET THE SECOND ONE TO SHOW
//IWebDriver driver = new ChromeDriver(chromeDriverService, option);
IWebDriver driver = new ChromeDriver();
driver.Url = "**************";
driver.Manage().Window.Maximize();
driver.SwitchTo().DefaultContent();
//Log-in
driver.FindElement(By.Id("username")).SendKeys("*****");
driver.FindElement(By.Id("password")).SendKeys("******" + OpenQA.Selenium.Keys.Enter);
//Entering Access Code
driver.FindElement(By.Id("password")).SendKeys("*******");
driver.FindElement(By.Id("accesscode")).SendKeys("********" + OpenQA.Selenium.Keys.Enter);
//go to CustomerList
driver.Navigate().GoToUrl("***********");
driver.Navigate().GoToUrl("*****************");
//Wait till load 3 seconds
waitOnPage(2);
DataTable dt = new DataTable();
var header = driver.FindElement(By.CssSelector("#gridComponent > div.k-grid-header"));
foreach (var row in header.FindElements(By.TagName("tr")))
{
//Configure Number of Col and row
int cellIndex = 0;
string[] arr = new string[32];
//Get Cell Data
foreach (var cell in row.FindElements(By.TagName("th")))
{
// Check the header cell for a checkbox child. If no
// such child exists, add the column.
var headerCheckboxes = cell.FindElements(By.CssSelector("input[type='checkbox']"));
if (headerCheckboxes.Count == 0)
{
//Number of Col Data Load
if (cellIndex <= 29)
{
arr[cellIndex] = cell.Text;
dt.Columns.Add(cell.Text);
}
else
cellIndex++;
}
}
Console.WriteLine(arr);
}
var table = driver.FindElement(By.CssSelector("#gridComponent"));
List<List<string>> records = GetRecords(table);
// Supposing you want the footer information
var lastPageStr = table.FindElement(By.ClassName("k-pager-last")).GetAttribute("data-page");
var lastPage = Convert.ToInt16(lastPageStr);
// You can select other info lik this
// class="k-link k-pager-nav" data-page="1"
driver.FindElement(By.CssSelector("#gridComponent > div.k-pager-wrap.k-grid-pager.k-widget.k-floatwrap > ul > li:nth-child(3)")).Click();
// Cycle over the pages
for (int p = 0; p < (lastPage - 1); p++)
{
driver.FindElement(By.CssSelector("#gridComponent > div.k-pager-wrap.k-grid-pager.k-widget.k-floatwrap > a:nth-child(4) > span")).Click();
waitOnPage(2);
var rows = GetRecords(table);
records.AddRange(rows);
}
// Add all rows to DT
//dt.Rows.Add(records[4].ToArray());
foreach(var row in records)
{
dt.Rows.Add(row.ToArray());
}
dataGridView1.DataSource = dt;
}

How to read data fast from an excel and convert it to list from file stream

I am using EPPlus.
The excel I am uploading has column headers in row number 2 . And from row 4 onward it has the data which may vary up to 2k records.
The way I am doing it , it takes a lot of time for reading 2k records and putting to a list .
using (var excel = new ExcelPackage(hpf.InputStream))
{
var ws = excel.Workbook.Worksheets["Sheet1"];
//Read the file into memory
for (int rw = 4; rw <= ws.Dimension.End.Row; rw++)
{
if (!ws.Cells[rw, 1, rw, 24].All(c => c.Value == null))
{
int headerRow = 2;
GroupMembershipUploadInput gm = new GroupMembershipUploadInput();
for (int col = ws.Dimension.Start.Column; col <= ws.Dimension.End.Column; col++)
{
var s = ws.Cells[rw, col].Value;
if (ws.Cells[headerRow, col].Value.ToString().Equals("Existing Constituent Master Id"))
{
gm.cnst_mstr_id = (ws.Cells[rw, col].Value ?? (Object)"").ToString();
}
else if (ws.Cells[headerRow, col].Value.ToString().Equals("Prefix of the constituent(Mr, Mrs etc)"))
{
gm.cnst_prefix_nm = (ws.Cells[rw, col].Value ?? (Object)"").ToString();
}
}
lgl.GroupMembershipUploadInputList.Add(gm);
}
}
GroupMembershipUploadInputList is the list of objects of type GroupMembershipUploadInput that I am adding the excel values to after reading from the cell wise.
Can it be done faster ? What am I missing here ?
Please help to improve the performance.
You are making a lot iterations there, for each row, you visit each column twice. I assume that you only need those two values per row and if so the following code would reduce time drastically:
using (var excel = new ExcelPackage(hpf.InputStream))
{
var ws = excel.Workbook.Worksheets["Sheet1"];
int headerRow = 2;
// hold the colum index based on the value in the header
int col_cnst_mstr_id = 2;
int col_cnst_prefix_nm = 4;
// loop once over the columns to fetch the column index
for (int col = ws.Dimension.Start.Column; col <= ws.Dimension.End.Column; col++)
{
if ("Existing Constituent Master Id".Equals(ws.Cells[headerRow, col].Value))
{
col_cnst_mstr_id = col;
}
if ("Prefix of the constituent(Mr, Mrs etc)".Equals(ws.Cells[headerRow, col].Value))
{
col_cnst_prefix_nm = col;
}
}
//Read the file into memory
// loop over all rows
for (int rw = 4; rw <= ws.Dimension.End.Row; rw++)
{
// check if both values are not null
if (ws.Cells[rw, col_cnst_mstr_id].Value != null &&
ws.Cells[rw, col_cnst_prefix_nm].Value != null)
{
// the correct cell will be selcted based on the column index
var gm = new GroupMembershipUploadInput
{
cnst_mstr_id = (string) ws.Cells[rw, col_cnst_mstr_id].Value ?? String.Empty,
cnst_prefix_nm = (string) ws.Cells[rw, col_cnst_prefix_nm].Value ?? String.Empty
};
lgl.GroupMembershipUploadInputList.Add(gm);
}
}
}
I removed the inner column loop and moved it to the start of the method. There it is used to just get the columnindex for each field you're interested in. The expensive null check can now also be reduced. To fetch the value, all that is now needed is a simple index lookup in the row.

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

Categories

Resources