I am using DataGridView in C# being civil engineer.
I want the values from a particular row. If the same value appears in a row, I want to display a message. For example, if 1 appears multiple time in a row, i want to display a message on every 1.
Here is how I am doing it:
private void button4_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView2.ColumnCount - 1; i++)
{
if (CategoryCLO[i] == 1)
{
MessageBox.Show("Here i am");
}
else
{
MessageBox.Show("I am not");
}
}
}
private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == 1)
{
CategoryCLO.Add(int.Parse(
dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()));
}
}
Problem:
It works right only one time. If a user replaces a value in a cell in DataGridView, it adds up another value corresponding to the newly added index of list.
Explanation:
The problem is that when user changes value in DataGridView, it got adds up in the list, thus increase the index of list , I want that index of list should be fix to the column index. and any change in particular cell , should change in that particular index rather than adding up.
Question:
I want that value should be placed in the previous index rather than creating new. In short, I want the indexes of list should be fixed to column number. How can I do it? any direction?
Note: Column numbers are variable depending upon the user
Related
I've made the app that way, when you click on current row you get a new form and there you can edit the item, anyway if I click on
Column Names:ID, Name, Country I get the new form again with first record to edit But I don't want to click on Column names and load edit form I only want that to happen when you click on first row and than to the end. So I've made it that way:
if (advancedDataGridView.CurrentCell.RowIndex > 0)
{
editRecord.ShowDialog();
}
But I can't edit the first row now, any ideas how can I fix that?
if (advancedDataGridView.CurrentCell.RowIndex > 0)
Indexes start at 0. This condition ignores 0 and will only be valid when the index is greater than 0 (so starting at 1). If you case, because indexes start at 0, you need to use greater than or equal to 0 (>=).
EDIT:
To fix your issue to ignore the header cells:
private void DataGridView_OnCellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && !(sender is DataGridViewHeaderCell))
{
MessageBox.Show("content cell was clicked");
}
}
Note that you must use the CellClick event handler.
When I write a number in a label and press a button, it must show me a dynamical allocation: the first column is number current, then 4 columns with comboboxes. So, when I press 3 it might show 3 rows with number and comboboxes and so on. How can I make this allocation? with multidimensional array and list of objects
You need to add a FlowLayoutPanel to your Form and then add rows to it as below:
private void AddRow()
{
//First create and setup your controls that are supposed to be in a row:
ComboBox cb1 = new ComboBox(){ /* settings */ };
// Other Controls
//Then add them to the FlowLayoutPanel:
flowLayoutPanel1.Controls.Add(cb1);
// add other controls
// Then set a line break at the end of row:
flowLayoutPanel1.SetFlowBreak(cb4, true); // let's say cb4 is the last control of the row.
}
Then you can use the method like this:
int numRows;
bool success = int.TryParse(textBox1.text, out numRows); // lets say textBox1 contains number of rows.
if(success)
{
for(int i = 0; i < numRows; i++)
{
AddRow();
}
}
I have this windows application
1.- So, first when I Add a number it added to listbox1 but not to list 2. I need to be add ed to listo 2 to
2.- I need the numbers be added separately... For example if I add number 202, it split on 2 after 0 after 2
3.- I need add button for FIFO, but I don't know how can I program it.
4.-Finally compare one by one it with listbox1 with listbox2 with polindrome method, and if its palindrome show message box, say "they are polindrome", if not, say "number it's not palindrome.
private void button1_Click(object sender, EventArgs e)
{
int newvalue;
if (int.TryParse(textBox1.Text, out newvalue))
{
numeros.Add(newvalue);
listBox1.Items.Add(textBox1.Text);
}
else
MessageBox.Show("insert a number");
textBox1.Clear();
textBox1.Focus();
}
For dealing with this problem you should keep your main data in memory (List<int>) and manipulate them using different methods and show the result in the listboxes.
Separating your digits can be done this way :
List<int> SeparateDigits(int n)
{
var result = new List<int>();
while(n>0)
{
result.Add(n % 10);
n /= 10;
}
return result;
}
After calling this method you can add the list data to both your listboxes if it's what you want.
(sorry for being late)
Good luck.
I have a ToolStripMenuItem MouseEnter event:
private void recentFilesToolStripMenuItem_MouseEnter(object sender, EventArgs e)
{
for (int i = 0; i < lines.Length; i++)
{
ToolStripMenuItem s = new ToolStripMenuItem(lines[i]);
if (!recentFilesToolStripMenuItem.DropDownItems.ContainsKey(lines[i]))
recentFilesToolStripMenuItem.DropDownItems.Add(s);
}
}
Now I am using ContainsKey but before i tried only with Contains(s)
In both cases it keep adding the items over and over again to the DropDownItems.
Each time i move the mouse and Enter i see the items added again.
In this case lines is array of string contain paths and names of text files.
For example in lines index 0 i see: d:\mytext.txt
The problem is that it keep adding them over again when i enter with the mouse and i want them to be added only once.
First time i see when entering with the mouse:
d:\mytext.txt
e:\test.txt
c:\hello\hellowowrld.txt
Next time when I enter with the mouse I see it twice:
d:\mytext.txt
e:\test.txt
c:\hello\hellowowrld.txt
d:\mytext.txt
e:\test.txt
c:\hello\hellowowrld.txt
Then next time i see the same items 9 times and so on.
There are two ways to do this.
One, is that you create your ToolStripMenuItem like this:
new ToolStripMenuItem(lines[i], (Image)null, (EventHandler)null, lines[i]);
It's that fourth parameter that is the "key" for .ContainsKey(...), not the first parameter.
Two, you can do it this way:
if (!recentFilesToolStripMenuItem.DropDownItems
.Cast<ToolStripMenuItem>()
.Any(x => x.Text == lines[i]))
{
recentFilesToolStripMenuItem.DropDownItems.Add(s);
}
This second way searches the actual text.
I want to click the "chkCPTData" button to delete some rows of the datagridview "CPTData". I have hundreds of rows of data in the datagridview. The first time I click the button, no rows are deleted. Then I click another time, some of the rows are deleted. It takes me about 8 times to delete all rows I want to delete. How can I can delete the rows by clicking the button only once? Thanks!
private void chkCPTData_Click(object sender, EventArgs e)
{
for (int rows = 0; rows <= CPTData.Rows.Count - 2; rows++)
{
double SampleDepth =(double)System.Convert.ToSingle(CPTData.Rows[rows].Cells[0].Value);
if (SampleDepth > (double)System.Convert.ToSingle(analysisDepth.Text))
{
CPTData.Rows.RemoveAt(rows);
}
}
CPTData.Refresh();
}
Deleting rows while enumerating through them will throw off the index, so try going in reverse instead:
for (int rows = CPTData.Rows.Count - 2; rows >=0; --rows)
{
double SampleDepth =(double)System.Convert.ToSingle(CPTData.Rows[rows].Cells[0].Value);
if (SampleDepth > (double)System.Convert.ToSingle(analysisDepth.Text))
{
CPTData.Rows.RemoveAt(rows);
}
}
The problem is caused by the forward loop. In this way, when you delete a row the index rows points no more to the next row but to the row after the next.
For example, you are on rows=10 and you need to delete it, after that, the rows is incremented in the loop to 11 but at this point the offset 11 of the Rows array is occupied by the row that was at offset 12 before the delete. Effectively you are skipping a row in your check after every RemoveAt.
The usual way to solve it is to loop backward (starting from the end and going toward the first row)
private void chkCPTData_Click(object sender, EventArgs e)
{
for (int rows = CPTData.Rows.Count - 1; rows >=0; rows--)
{
double SampleDepth =(double)System.Convert.ToSingle(CPTData.Rows[rows].Cells[0].Value);
if (SampleDepth > (double)System.Convert.ToSingle(analysisDepth.Text))
{
CPTData.Rows.RemoveAt(rows);
}
}
CPTData.Refresh();
}