i want create search in form similar EXCEL , find and put (row) in listview
this my form :
and my code :
private int searchIndex = -1;
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "Find Next";
try
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
searchIndex = (searchIndex + 1) % dataGridView1.Rows.Count;
DataGridViewRow row = dataGridView1.Rows[searchIndex];
if (row.Cells["Product"].Value == null)
{
continue;
}
if (row.Cells["Product"].Value.ToString().Trim().StartsWith(textBox1.Text) || row.Cells["Product"].Value.ToString().Trim().Contains(textBox1.Text))
{
ListViewItem lvi = new ListViewItem(row.Cells["Product"].Value.ToString());
lvi.SubItems.Add(row.Cells["Product"].Value.ToString());
listView1.Items.Add(lvi);
dataGridView1.CurrentCell = row.Cells["Product"];
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[row.Index].Index;
row = dataGridView1.Rows[i];
return;
}
}
}
catch { }
}
and in textbox1_textchanged :
searchIndex = -1;
button1.Text = "Find";
listView1.Clear();
I want when search end, send message ...
thanks
Mimicking the Find All functionality, populating your ListView will be very similar to the Find Next functionality. Here's an example if it were a separate button from Find Next:
public Form1()
{
InitializeComponent();
listView1.View = View.Details;
listView1.Columns.Add("Column");
listView1.Columns.Add("Row");
listView1.Columns.Add("Value");
}
private void button2_Click(object sender, EventArgs e)
{
button2.Text = "Find All";
int tempIndex = -1;
listView1.Items.Clear();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
tempIndex = (tempIndex + 1) % dataGridView1.Rows.Count;
DataGridViewRow row = dataGridView1.Rows[tempIndex];
if (row.Cells["Foo"].Value == null)
{
continue;
}
if (row.Cells["Foo"].Value.ToString().Trim().StartsWith(textBox1.Text) || row.Cells["Foo"].Value.ToString().Trim().Contains(textBox1.Text))
{
DataGridViewCell cell = row.Cells["Foo"];
ListViewItem lvRow = new ListViewItem(new string[] { cell.ColumnIndex.ToString(), cell.RowIndex.ToString(), cell.Value.ToString() });
listView1.Items.Add(lvRow);
}
}
MessageBox.Show("Search ended."); // Or whatever message you intend to send.
}
Related
I wanted to do select/ deselect with one checkbox. I have tried to get the index of the selected item. I haven't been able to get this, so I tried to change the name of the box. Still no success.
foreach (var item in DATAsetname_INIlist)
{
checkedListBox2.Items.Add(item);
}
if (checkedListBox2.Items.Count != 0)
{
checkedListBox2.Items.Add("Select all");
}
private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (checkedListBox2.Items.Count != 0)
{
if (checkedListBox2.SelectedItem.ToString() == "Select all")
{
for (int i = 0; i < checkedListBox2.Items.Count; i++)
{
checkedListBox2.SetItemChecked(i, true);
}
string changed = "Deselect all";
checkedListBox2.SelectedItem =changed;
}
if (checkedListBox2.SelectedItem.ToString() == "Deselect all")
{
for (int i = 0; i < checkedListBox2.Items.Count; i++)
{
checkedListBox2.SetItemChecked(i, false);
}
string changed = "Select all";
checkedListBox2.SelectedItem = changed;
}
}
}
can you please help on this ? thank you
Have done quick exercise. Please modify your code accordingly.
/// <summary>
/// Check all check boxes and vice versa
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ChkSelectAll_CheckedChanged(object sender, EventArgs e)
{
//Declare your checkedListBox2 count
iCount = checkedListBox2.Items.Count;
if (sender != null)
{
for (int i = 1; i <= iCount; i++)
{
CheckBox ck = null;
Control[] chkTest = this.Controls.Find("chkDrive" + i, true);
if (chkTest.Length > 0)
{
if (chkSelectAll.Checked)
{
for (int j = 0; j < chkTest.Length; j++)
{
ck = (CheckBox)chkTest[j];
ck.Checked = true;
}
}
else
{
for (int j = 0; j < chkTest.Length; j++)
{
ck = (CheckBox)chkTest[j];
ck.Checked = false;
}
}
}
}
}
}
can you try this.
try
{
checkedListBox1.SelectedIndexChanged -= checkedListBox1_SelectedIndexChanged;
if (checkedListBox1.Items.Count != 0)
{
if (checkedListBox1.SelectedItem.ToString() == "Select all")
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}
string changed = "Deselect all";
checkedListBox1.Items[checkedListBox1.SelectedIndex] = changed;
}
else if (checkedListBox1.SelectedItem.ToString() == "Deselect all")
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, false);
}
string changed = "Select all";
checkedListBox1.Items[checkedListBox1.SelectedIndex] = changed;
}
}
}
catch (Exception ex)
{
}
finally
{
checkedListBox1.SelectedIndexChanged += checkedListBox1_SelectedIndexChanged;
}
If this is C# based, windows application.
Search form collection and find all check box on form, using for each.
Code
foreach(CheckBox item in Form.Control)
{
Item.Checked=true;
}
For check list box
for(int i;I<checklistbox.items.count;I++)
{
checklistbox.SetItemChecked(I,true);
}
When i work with c# i had litle simillar problem in my case was enought was to replace if(string==string) by string.equals(string)
Btw few questions:
checkedListBox2.SelectedItem.ToString() what is value in here where comparing?
Does your listener work at all?
Not better way to check which checkBox is taken comparing sender in switch?
How u declare checkedListBox2
By checkedListBox2.Items.Add(item); i suspect u create checkBoxes by hand can u show it? meybe something is there wrong
This part added after Lian comment
foreach(var item in DATAsetname_INIlist)
{
checkedListBox2.Items.Add(item);
}
if (checkedListBox2.Items.Count != 0) {
checkedListBox2.Items.Add("Select all");
}
private void checkedListBox2_SelectedIndexChanged (object sender, EventArgs e)
{
if (checkedListBox2.Items.Count != 0 && checkedListBox2.SelectedItem.ToString().equals("Select all")) {
changeStateOfSelectedItem("Deselect all", true);
} else if (checkedListBox2.SelectedItem.ToString().equals("Deselect all")) {
changeStateOfSelectedItem("Select all", false);
}
}
private void changeStateOfSelectedItem (String state, bolean stateToReplace){
for (int i = 0; i < checkedListBox2.Items.Count; i++) {
checkedListBox2.SetItemChecked(i, stateToReplace);
}
string changed = state;
checkedListBox2.SelectedItem = changed;
The Print Preview controller shows the first page not the 2n or 3rd.. pages.
Show_Page() method displays the all pages without problem in a list view.
The method that I use for changing the pages print preview controller is as below:
What should I change or add for displaying next and previous pages ?
private void nxtBtn_Click(object sender, EventArgs e)
{
if (PrevIndex < PgCount)
++PrevIndex;
if (PrevIndex == PgCount - 1)
nxtBtn.Enabled = false;
prvBtn.Enabled = true;
ppd.PrintPreviewControl.InvalidatePreview();
fName = GetFName();
if (PublicVariables.PrintData == 2)
Show_Page();
else
{
pd.DocumentName = fName;
ppd.Document = pd;
ppc.Document = pd;
ppc.Update();
}
label2.Text = (PrevIndex + 1).ToString();
}
private void ShowPage()
{
streamToRead = new StreamReader(fName, Encoding.UTF8);
string line;
int LineNbr = 0;
li.Items.Clear();
LineNbr = File.ReadAllLines(fName).Length;
li.View = View.Details;
int counter = 0;
ListViewItem Lvi = new ListViewItem();
char sep='|';
int ctr_limit=0;
if (PublicVariables.Grup_It == 0)
ctr_limit = 9;
else
ctr_limit = 7;
string[] tmp1 = new string[ctr_limit];
while (counter < LineNbr && (line = streamToRead.ReadLine()) != null)
{
if (PublicVariables.PrintData == 2 && counter < 3)
goto NextLine;
string[] tmp = line.Split(sep);
for (int i = 0; i < ctr_limit; ++i)
{
if (PublicVariables.Grup_It > 0)
tmp1[i] = tmp[i + 1];
else
tmp1[i] = tmp[i];
}
Lvi = new ListViewItem(tmp1);
li.Items.Add(Lvi);
NextLine:
++counter;
}
streamToRead.Close();
}
private void nxtBtn_Click(object sender, EventArgs e)
{
if (PrevIndex < PgCount)
++PrevIndex;
if (PrevIndex == PgCount - 1)
nxtBtn.Enabled = false;
prvBtn.Enabled = true;
ppd.PrintPreviewControl.InvalidatePreview();
fName = GetFName();
if (PublicVariables.PrintData == 2)
Show_Page();
else
{
pd.DocumentName = fName;
ppd.Document = pd;
ppc.Document = pd;
ppc.InvalidatePreview();
}
label2.Text = (PrevIndex + 1).ToString();
}
Instead of ppc.Update() I have to write ppc.InvalidatePreview();
That permits to show the next page.
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.
I have a datagridview1 on a form, which contains number of columns and data. Here I need to find specific cells which contains the decimal value of "0.04" (for example), must be replaced with the numeric value of "0" by clicking single button.
I have tried already this,
private void button1_Click(object sender, EventArgs e)
{
string filterBy;
filterBy = "Stringtext Like '%" + 0.04 + "%'";
((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = filterBy;
int rows = dataGridView1.Rows.Count;
for (int i = 0; i < rows-1; i++)
{
dataGridView1[2, i].Value = dataGridView1[2, i].Value.ToString().Replace(0.04, 0);
}
dataGridView1.Refresh();
}
no use, can anyone help..?
thanks in advance..
I wouldnt do it the hard way, like you. This is how i would do it:
private void button1_Click(object sender, EventArgs e)
{
int countColumn, intCell = -1, intRow = -1;
countColumn = dataGridView1.Columns.Count;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
intRow++;
foreach (DataGridViewCell c in row.Cells)
{
//if (c.Value == null) { continue; }
if (c.Value?.ToString() != "0.04") { continue; }
intCell = +1;
dataGridView1.Rows[intRow].Cells[intCell].Value = "0";
if (intCell == countColumn)
{
intCell = -1;
}
}
}
}
I have a Button that creates a list of TextBoxes Dynamically and I also have a Button that submits the information. However I don't know how to access the values of the Textboxes. Below is the code:
if (IsPostBack)
{
ViewState["count"] = Convert.ToInt32(ViewState["count"]) + 1;
int Count = int.Parse(string.Format("{0}", ViewState["count"]));
var lstTextBox = new List<TextBox>();
for (int i = 0; i < Counter; i++)
{
TextBox txtbx = new TextBox();
txtbx.ID = string.Format("txtbx{0}", i);
// txtbx.AutoPostBack = true;
lstTextBox.Add(txtbx);
//txtbx.Text = "initial value";
}
Session["lstTextBox"] = lstTextBox;
}
protected void Button1_Click(object sender, EventArgs e)
{
int total = Counter;
for (int i = 0; i < total; i++)//Calls to createbox
CreateTextBox(i);
//Label1.Text = Counter.ToString();
if (Counter == 4)
{
Button1.Visible = false;
}
}
private int Counter
{
get { return Convert.ToInt32(ViewState["count"] ?? "0"); } //Fields button counter
set { ViewState["count"] = value; }
}
private void CreateTextBox(int j) //Creates the fields / cells
{
var box = new TextBox();
box.ID = "Textbox" + j;
box.Text = "Textbox" + j;
var c = new TableCell();
c.Controls.Add(box);
r.Cells.Add(c);
table1.Rows.Add(r);
}
How would like to have Button2 grab the values.
Thank you in advance!!
do it like this
foreach(Control c in YourControlHolder.Controls)
{
if(c is TextBox)
{
//your code here.
}
}