Setting focus to a DataGridView row - c#

The following source code is intended to set focus to the immediately previous row of a deleted row.
Suppose I want to delete an unwanted word dddddddd from the database. When I press the Delete button, I want the word cynosure to be focused and placed at the top of the DataGridView which is not the case right now.
Right now, it is displayed at the bottom.
Source Code
void SetFocusToWord(Word concernedWord)
{
if (concernedWord != null)
{
int index = 0;
foreach (DataGridViewRow r in dataGridView1.Rows)
{
Word item = r.Tag as Word;
if (concernedWord.Name == item.Name)
{
dataGridView1.Focus();
dataGridView1.CurrentCell = dataGridView1.Rows[index].Cells[0];
break;
}
index++;
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (dataGridView1.SelectedRows.Count > 0)
{
int selectionIndex = dataGridView1.SelectedRows[0].Index;
foreach (DataGridViewRow r in dataGridView1.SelectedRows)
{
Word c = r.Tag as Word;
if (c != null)
{
_wordDatabase.Delete(c);
}
}
LoadToDataGridView();
if(selectionIndex > 0)
{
selectionIndex = selectionIndex - 1;
}
Word item = dataGridView1.Rows[selectionIndex].Tag as Word;
SetFocusToWord(item);
}
else
{
throw new Exception(SelectionErrorMessages.GetErrorMessageFor(typeof(Word)));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void LoadToDataGridView()
{
dataGridView1.Rows.Clear();
List<Word> items = (List<Word>)_wordDatabase.Get();
if (items != null)
{
if (items.Count > 0)
{
int i = 0;
foreach (Word c in items)
{
dataGridView1.Rows.Add(c.Name, c.Hint);
dataGridView1.Rows[i].Tag = c;
++i;
}
}
}
}

Reloading the database seems like an unnecessary step.
Based on how you want the grid to behave, try using code like this:
if (dataGridView1.SelectedRows.Count > 0) {
int selectIndex = dataGridView1.SelectedRows[0].Index;
dataGridView1.Rows.RemoveAt(selectIndex);
if (selectIndex > 0) {
dataGridView1.ClearSelection();
dataGridView1.Rows[selectIndex - 1].Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = selectIndex - 1;
}
}

Poor performance and abundance of unnecessary code in your project are the result of you not using winforms binding properly. I suggest you start utilizing control's BindingContext and BindingSource accordingly. This should solve most of your problems.
For further details on winforms binding I recommend this docs.

Related

DataGridView C# windows application(First row not selecting which is checkbox if mouse click on column Header)

Can please any one help me on this. I have developed a c# windows application which has DataGridView first column has checkboxes. if I click on first column header it selects all the row level check boxes except the first row. For selecting all row level check boxes I have an event of dataGridView1_ColumnHeaderMouseClick and the code is:
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
column.SortMode = DataGridViewColumnSortMode.NotSortable;
}
if (e.ColumnIndex == 0)
{
if (chek == 0)
{
try
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
string paymentValue = dataGridView1.Rows[i].Cells[18].Value.ToString();
string incmngp = dataGridView1.Rows[i].Cells[20].Value.ToString();
if (paymentValue == "N" && incmngp =="")
{
dataGridView1.Rows[i].Cells[0].Value = 1;
chek = 1;
}
}
if (chek == 1)
{
btn_update.Text = "Update";
}
}
catch (Exception ) { }
}
else if(chek==1)
{
try
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
dataGridView1.Rows[i].Cells[0].Value = 0;
chek = 0;
}
if (chek == 0)
{
btn_update.Text = "OK";
}
}
catch (Exception) { }
}
}
Note: chek is the variable declared on initialize stage
Set your Selection mode property of data grid view to ColumnHeaderSelect
and make sure all your 'Text' columns have SortMode set to NotSortable
UPDATE 2
In which case, Undo everything I ever told before and do that like this
Before you are assigning any DataTable to dataGridView1.
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
dataGridView1.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
foreach(DataGridViewColumn dc in dataGridView1.Columns)
{
dc.SortMode = DataGridViewColumnSortMode.NotSortable;
}
dataGridView1.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
UPDATE 3
Add an event handler for your dataGridView1's ColumnHeaderMouseClick Event
like below
Add the below code (Generic code if you want to use the same functionality for any column of check boxes)
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
//Enter your own column index here
if(e.ColumnIndex == 0)
foreach(DataGridViewRow row in dataGridView1.Rows)
foreach (DataGridViewCell cell in row.Cells)
{
//Check if the cell type is of a CheckBoxCell
if (cell.GetType() == typeof(DataGridViewCheckBoxCell))
{
DataGridViewCheckBoxCell c = (DataGridViewCheckBoxCell)cell;
c.TrueValue = "T";
c.FalseValue = "F";
if (c.Value == c.FalseValue|| c.Value == null )
c.Value = c.TrueValue;
else
c.Value = c.FalseValue;
}
}
dataGridView1.RefreshEdit();
}
This is a very bizarre bug in Winforms. The problem more generally applies not to the first row, but to the first selected cell in any row of DataGridViewCheckBoxCell(s). You can select the CheckBox cell by clicking on the check box, or select the cell outside the check box, the behavior is the same. If you select 3 check boxes in the middle of your grid, the first of those three will freeze and not update properly. If you try to clear the selection in code, with a dataGridView1.ClearSelection() method call, it still does not work.
The correct answer is to call datagridview1.RefreshEdit() right after you change the checkbox data. You can't just call it after all changes are made. It must be made for each change in the CheckBox value.
foreach (DataGridViewRow row in Results.Rows)
{
var ck = (DataGridViewCheckBoxCell) row.Cells["check"];
ck.Value = ck.TrueValue;
Results.RefreshEdit();
}

hide empty row in gridview

I want to hide empty row in one particular column. I tried to but negative. Below is my code:
protected void gvDb_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow rw in gvDb.Rows)
{
if ((string.IsNullOrEmpty(rw.Cells[1].Text) | (rw.Cells[1].Text == "")))
{
rw.Visible = false;
}
}
}
for (int i = 0; i < gvDb.RowCount - 1; i++)
{
var row = gvDb.Rows[i];
if (string.IsNullOrEmpty(Convert.ToString(row.Cells[1].Value)))
{
row.Visible = false;
}
}
This will work,
use for instead of foreach to iterate all the rows except last row which is empty.

How to count duplicates from ListBox and show the number of duplicated items beside it?

So I have a button, where if you click it, it adds "Candy" to a listbox, how do I make it so, if another item with the same name is being added, instead of adding it in a new line, update the first line to show x2, 3, 4, etc. Is that possible or would I have to make another Listbox and match the index? I've tried the following with another listbox and an int variable.
private void btnCandy_Click(object sender, EventArgs e)
{
lstProducts.Items.Add("Candy");
foreach (var item in lstProducts.Items)
{
if (item.ToString() == "Candy")
{
++Productcount;
lstQuantity.Items.Add(Productcount);
if (Productcount > 1)
{
lstQuantity.Items.Insert(lstProducts.Items.IndexOf("Candy"), Productcount);
}
}
}
}
using System.Text.RegularExpressions;
Use:
private void btnCandy_Click(object sender, EventArgs e)
{
string query = "Candy";
bool isExist = false;
for (int i = 0; i < lstProducts.Items.Count; i++)
{
var s = lstProducts.Items[i].ToString();
if (s.StartsWith(query))
{
if (s == query)
{
lstProducts.Items[i] = query + "x2";
isExist = true;
break;
}
else
{
// Escape your plain text before use with regex
var pattern = Regex.Escape(query);
// Check if s has this formnat: queryx2, queryx3, queryx4, ...
Match m = Regex.Match(s, "^" + pattern + #"x(\d+)$");
if (m.Success)
{
lstProducts.Items[i] = query + "x" + (Int32.Parse(m.Groups[1].Value) + 1);
isExist = true;
break;
}
}
}
}
if (!isExist) lstProducts.Items.Add(query);
}
Note:
\d mean any digit (0 - 9)
I'd try to iterate through listbox items and if I find "Candy" then take that index and update title.
private void btnCandy_Click(object sender, EventArgs e)
{
bool found = false;
foreach (var item in lstProducts.Items)
{
if (item.ToString().StartsWith("Candy"))
{
// update item title
found = true;
break; // no need to continue
}
}
if(!found)
{
lstProducts.Items.Add("Candy");
}
}
this way you are not going to add duplicates
Here is some pseudo-code to help you. Add this to your button click event:
int i = 0;
foreach (string item in listbox1.Items)
{
If (item == textbox1.text) //textbox1.text contains the string such as 'candy'
{
i++;
listbox1.Items.Remove(item);
listbox1.Items.Add(textbox1.text + " " + i.ToString());
}
}
You may have to reset the counter as needed.

How to Get a Cell Content of DataGrid which is not Selected in WPF?

1. how to Calculate a Price Cell.
2. their is no record is selected.
3. At end it show sum of price cell.
private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
try
{
string s = dataGrid1.Columns[2].GetCellContent(e.Row).ToString();
//give me a Null Reference Error
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Try this :
int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value);
}
LoadingRow event gives you the current DataGridRow, so you need to access the corresponding Item to that DataGridRow.
So, Change this
string s = dataGrid1.Columns[2].GetCellContent(e.Row).ToString();
to something similar to :
MyItem item = Dgrd.ItemContainerGenerator.ItemFromContainer(e.Row) as MyItem;
if (item != null)
{
System.Diagnostics.Debug.WriteLine(item.Name);
}
or,
MyItem i = e.Row.DataContext as MyItem;
if (item != null)
{
System.Diagnostics.Debug.WriteLine(item.Sku);
}

One row of list view is updating, but duplicate row is also inserted

Hello folks I am trying to update the quantity of a product that is being ordered. If the product already exists in the users 'basket' instead of inserting a new row, the quantity in the row with the existing item should be updated. It is updating but a new row is also inserted with the quantity that should have been added to the original row, like so:
I'm guessing something is wrong logically in my code but, I can't spot it.
private void btn_add_Click(object sender, EventArgs e)
{
try
{
ListViewItem item = new ListViewItem(list_Select_Product.SelectedItems[0].Text);
item.SubItems.Add(list_Select_Product.SelectedItems[0].SubItems[1].Text);
item.SubItems.Add(txt_quantity.Text);
bool ok = true;
if (!validNumbers(txt_quantity))
ok = false;
if (!validLength(txt_quantity, 1, 2))
ok = false;
if (ok == true)
{
foreach (ListViewItem lvi in list_view_orderitems.Items)
{
if(lvi.SubItems[0].Text == list_Select_Product.SelectedItems[0].Text)
{
int UpdateQunat = Convert.ToInt32(lvi.SubItems[2].Text);
int AddMe = Convert.ToInt32(txt_quantity.Text);
UpdateQunat = UpdateQunat + AddMe;
lvi.SubItems[2].Text = Convert.ToString(UpdateQunat);
list_view_orderitems.Items.Add(item);
}
else if (lvi.SubItems[0].Text != list_Select_Product.SelectedItems[0].Text)
{
list_view_orderitems.Items.Add(item);
}
}
if(list_view_orderitems.Items.Count == 0)
{
list_view_orderitems.Items.Add(item);
}
}
}
catch
{
MessageBox.Show("A product must be selected");
}
}
Look a little closer in the foreach loop. You actually add it there as well. (So you should remove that)
foreach (ListViewItem lvi in list_view_orderitems.Items)
{
if(lvi.SubItems[0].Text == list_Select_Product.SelectedItems[0].Text)
{
int UpdateQunat = Convert.ToInt32(lvi.SubItems[2].Text);
int AddMe = Convert.ToInt32(txt_quantity.Text);
UpdateQunat = UpdateQunat + AddMe;
lvi.SubItems[2].Text = Convert.ToString(UpdateQunat);
// adding it again. This line is not needed.
list_view_orderitems.Items.Add(item);
}
else if (lvi.SubItems[0].Text != list_Select_Product.SelectedItems[0].Text)
{
list_view_orderitems.Items.Add(item);
}
}
I this that it this if you need to get rid of the item.Add (i marked it:
if(lvi.SubItems[0].Text == list_Select_Product.SelectedItems[0].Text)
{
int UpdateQunat = Convert.ToInt32(lvi.SubItems[2].Text);
int AddMe = Convert.ToInt32(txt_quantity.Text);
UpdateQunat = UpdateQunat + AddMe;
lvi.SubItems[2].Text = Convert.ToString(UpdateQunat);
// list_view_orderitems.Items.Add(item);
}

Categories