Datagrid Column Selected - c#

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

Related

How to sum datagridview selected cell CORRECTLY

it's working but not correctly
... if selecting from up to down it's sum correct somewhere, but Down to up select doesnt summing
private void SalesGridView_SelectionChanged(object sender, EventArgs e)
{
float sumNumbers = 0;
for (int i = 0; i < SalesGridView.SelectedCells.Count; i++)
{
if (!SalesGridView.SelectedCells.Contains(SalesGridView.Rows[i].Cells["TotalBillColumn"]))
{
float nextNumber = 0;
if (float.TryParse(SalesGridView.SelectedCells[i].FormattedValue.ToString(), out nextNumber))
sumNumbers += nextNumber;
label1.Text = "selected value " + sumNumbers;
label2.Text = "nr selected cells " + SalesGridView.SelectedCells.Count.ToString();
}
else
{
}
}
}
Try the following code:
private void SalesGridView_SelectionChanged(object sender, EventArgs e)
{
float sumNumbers = 0;
for (int i = 0; i < SalesGridView.SelectedCells.Count; i++)
{
var selectedCell = SalesGridView.SelectedCells[i];
if (selectedCell.ColumnIndex == SalesGridView.Columns["TotalBillColumn"].Index)
{
float nextNumber = 0;
if (float.TryParse(selectedCell.FormattedValue.ToString(), out nextNumber))
sumNumbers += nextNumber;
}
}
label1.Text = "selected value " + sumNumbers;
label2.Text = "nr selected cells " + SalesGridView.SelectedCells.Count.ToString();
}
Sorry, for the late explanation, when I wrote that answer didn't have time to elaborate what your actual problem is and only gave you a solution that should work.
Your problem can be located at that line:
if (!SalesGridView.SelectedCells.Contains(SalesGridView.Rows[i].Cells["TotalBillColumn"]))
To be precise the following statment will give you wrong results:
SalesGridView.Rows[i]
You will receive unwanted results, because i doesn't fit your requirement of an index of your Rows, so lets look at what you wrote in your for loop.
for (int i = 0; i < SalesGridView.SelectedCells.Count; i++)
Your for loop tries to enumerate the SelectedCells of your SalesGridView.
So lets assume you select 5 cells, than your for loop with enumerate from 0 to exclusive 5, so i will be 0, 1, 2, 3 or 4.
Now lets check again with the problematic statement I mentioned earlier:
SalesGridView.Rows[i]
Does the SelectedCell with for example i = 0 correspone with Rows[0] no it doesn't have to.
Your SelectedCells could be of any row and your for loop will always enumerate from 0 to SelectedCells.Count but your statementment of SalesGridView.Rows[i] will always try to get the Row with the given index of i.
So, for example:
You select 2 cells in row 6 and 7.
SelectedCells.Count will have 2
You enumerate SelectedCells beginning from 0 to exclusive 2, so i will be 0 or 1
Inside your loop you try to access the row with i, by calling SalesGridView.Rows[i]
as mentioned in point 3. i will only be 0 or 1, but I selected 2 cells of the rows 6 and 7
private void button1_Click(object sender, EventArgs e)
{
double result = 0;
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
result = result + double.Parse(cell.Value.ToString());
}
label2.Text = result.ToString();
}
and with TryParse if you want to go on the safe side:
private void button1_Click(object sender, EventArgs e)
{
double result = 0;
double nextNumber = 0;
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
if (double.TryParse(cell.Value.ToString(), out result))
{
nextNumber += result;
label2.Text = nextNumber.ToString();
}
}
}

Want to check whether a gridview contain duplicate value

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

How to transfer selected rows from one datagridview to another?

I try to transfer selected rows from one datagridview to another. I used combobox textchange event to load selected data to datagrid. So that transfer code need to do dataGridView1.AllowUserToAddRows = false; for both dataGridViews. But due to use combobox textchange event I can't disable adding new rows. How to solve this? I use following code to transfer values:
private void btnaddtoanother_Click(object sender, EventArgs e)
{
for (int i = 0; i <= dataGridView2.Rows.Count - 1; i++)
{
bool rowAlreadyExist = false;
bool checkedCell = (bool)dataGridView2.Rows[i].Cells[0].Value;
if (checkedCell == true)
{
DataGridViewRow row = dataGridView2.Rows[i];
if (dataGridView1.Rows.Count != 0)
{
for (int j = 0; j <= dataGridView1.Rows.Count - 1; j++)
{
if (row.Cells[0].Value.ToString() == dataGridView1.Rows[j].Cells[0].Value.ToString())
{
rowAlreadyExist = true;
break;
}
}
if (rowAlreadyExist == false)
{
dataGridView2.Rows.Add(row.Cells[0].Value.ToString());
dataGridView2.Rows.Add(row.Cells[1].Value.ToString());
dataGridView2.Rows.Add(row.Cells[3].Value.ToString());
}
}
else
{
dataGridView2.Rows.Add(row.Cells[0].Value.ToString());
dataGridView2.Rows.Add(row.Cells[1].Value.ToString());
dataGridView2.Rows.Add(row.Cells[3].Value.ToString());
}
}
}
}
Start with removing the row from the Grid before you try to add it to the new Grid.
foreach (GridViewRow row in fromGridView.SelectedRows.OfType<DataGridViewRow>().ToArray())
{
fromGridView.Rows.Remove(row);
toGridView.Rows.Add(row);
}

How do I create search like excel in DataGridView?

I used this code to search in DataGridView to find and select a row (no filter)! But, when DataGridView has repetitive values in rows it won't get the next row! How do I go to the next row with every click to Btn_find (Find similar to Excel)?
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "Find Next";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells["ProductId"].Value == null)
{
continue;
}
if (row.Cells["ProductId"].Value.ToString().Trim() == textBox1.Text)
{
dataGridView1.CurrentCell = row.Cells["ProductId"];
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[row.Index].Index;
return;
}
}
}
The idea:
Add a private field to the class as an index to remember which row you last found.
Reset this index if the search text is changed. (Optional)
Iterate through all rows, beginning with the row following the last successful search. If no results were found last search, start at the beginning.
Implementation:
private int searchIndex = -1;
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "Find Next";
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
searchIndex = (searchIndex + 1) % dataGridView1.Rows.Count;
DataGridViewRow row = dataGridView1.Rows[searchIndex];
if (row.Cells["Foo"].Value == null)
{
continue;
}
if (row.Cells["Foo"].Value.ToString().Trim() == textBox1.Text)
{
dataGridView1.CurrentCell = row.Cells["Foo"];
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[row.Index].Index;
return;
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
searchIndex = -1;
}
Why these changes?
for (int i = 0; i < dataGridView1.Rows.Count; i++)
Still iterate through every row.
searchIndex = (searchIndex + 1) % dataGridView1.Rows.Count;
Start at row[searchIndex + 1]. When we reach the last row, mod (%) returns us to the first row.
private void textBox1_TextChanged(object sender, EventArgs e)
{
searchIndex = -1;
}
Start back at the beginning of the list when new search criteria are entered. This is optional.

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