So this is a hard one, i am doing a mangment software for a warehouse and i save the location of items on the shelves as plain number in my database, ie i have column row,shelf and ledge, and each one is a number (the total is of each is determined first) to say i have 11 rows, 5 shelves and each shelf has 5 ledges.
now my problem is i locate an item in row 1 shelf 1 and ledge 1, all good now what i want to do is for the program to detect that there is already an item in that location and when i try to save another item it just disables out or just removes that option in the combobox.
here is how i load the comboboxes
dgvAux.DataSource = datos.consultar("select top 1 rows from warehouse");
int rows= Convert.ToInt32(dgvAux.Rows[0].Cells[0].Value.ToString());
for (int i = 1; i < rows+ 1; i++)
{
ComboboxItem item = new ComboboxItem();
item.Text = "Row " + i.ToString();
item.Value = i;
cboRow.Items.Add(item);
cboRow.SelectedIndex = 0;
}
dgvAux.DataSource = datos.consultar("select top 1 shelves from warehouse");
int shelves= Convert.ToInt32(dgvAux.Rows[0].Cells[0].Value.ToString());
for (int i = 1; i < shelves+ 1; i++)
{
ComboboxItem item = new ComboboxItem();
item.Text = "Shelf" + i.ToString();
item.Value = i;
cboShelves.Items.Add(item);
cboShelves.SelectedIndex = 0;
}
dgvAux.DataSource = datos.consultar("select top 1 ledges from almacenes");
int ledges= Convert.ToInt32(dgvAux.Rows[0].Cells[0].Value.ToString());
for (int i = 1; i < ledges+ 1; i++)
{
ComboboxItem item = new ComboboxItem();
item.Text = "Ledge" + i.ToString();
item.Value = i;
cboLedge.Items.Add(item);
cboLedge.SelectedIndex = 0;
}
i know its not the best way to do it, but is the way i was able to make it work
You could have an event handler on each time a combobox is changed that will check the availability of all options in further comboboxesLike so if you choose a shelf number, a check is done for all the available space in that shelf and will remove the irrelevant or already full spaces (if statement or switch case to add and remove items from comboboxes)
As guys are pointing out in the comments, it is not a good design to have disabled items in combo list.
However there is property IsEnabled which you need to set to False and that's it. You need to have algorithm to decide which one to set to disable.
Related
private void DisplayTableWithListview()
{
listView1.GridLines = true;// Whether the grid lines are displayed
listView1.FullRowSelect = true;// Whether to select the entire line
listView1.View = View.Details;// Set display mode
listView1.Scrollable = true;// Whether to show the scroll bar automatically
listView1.MultiSelect = false;// Is it possible to select multiple lines
// Add header(column)
listView1.Columns.Add("ToString(yyyyMMddHHmm)", 160, HorizontalAlignment.Center);
// Add items into table
for (int i = 0; i < 6; i++)
{
ListViewItem item = new ListViewItem();
item.SubItems.Clear();
item.SubItems[0].Text = "Product" + i.ToString();
item.SubItems.Add(i.ToString());
item.SubItems.Add((i + 7).ToString());
item.SubItems.Add((i * i).ToString());
listView1.Items.Add(item);
}
}
Product0 is in the cell/place of the column header.
i want the items to be start added from the second cell.
you can use listView1.Items.Insert(index , item) to add item in specialty index. listview.listviewitemcollection.insert()
listView1.Items.Insert(2,"yourItem");
if you want the items to be start added from the second cell try this:
listView1.Items.Add("");
for (int i = 0; i < 6; i++)
{
ListViewItem item = new ListViewItem();
item.SubItems.Clear();
item.SubItems[0].Text = "Product" + i.ToString();
item.SubItems.Add(i.ToString());
item.SubItems.Add((i + 7).ToString());
item.SubItems.Add((i * i).ToString());
listView1.Items.Add(item);
}
I Inerted a DGV called MarksDGV1 into my application while each cell inside of it has a default value of "0". So, after the user changes the value of some of them, when I try to reach the value for the last edited cell it gives me 0 instead of what the user typed even though it's shown correctly
(Please note: the unselected cells -which doesn't appear in Blue color- show value correctly)
How could I fix that?
Here is my code:
MarksDGV1.Refresh();
MessageBox.Show(MarksDGV1.Rows[0].Cells[1].Value.ToString());
And this is How I built the DGV:
using (DataGridViewTextBoxColumn tmp = new DataGridViewTextBoxColumn())
{
tmp.Width = 90;
tmp.ReadOnly = true;
tmp.HeaderText = "פרק מס.";
MarksDGV1.Columns.Add(tmp);
}
for (int i = 1; i <= 30; i++)
{
using (DataGridViewTextBoxColumn tmp = new DataGridViewTextBoxColumn())
{
tmp.Width = 50;
tmp.HeaderText = "שאלה מס." + i;
MarksDGV1.Columns.Add(tmp);
}
}
for (int i = 0; i < 8; i++)
{
using (DataGridViewRow tmp = (DataGridViewRow)MarksDGV1.Rows[i].Clone())
{
tmp.Cells[0].Value = i + 1;
for (int j = 1; j <= 30; j++)
{
tmp.Cells[j].Value = 0;
//tmp.Cells[j].Value = CurrentExam.Psy[i].Answers[j - 1];
}
MarksDGV1.Rows.Add(tmp);
}
}
Update: I tried typing DataGridView.Refresh(); but didn't work!
Update2: I was able to fix this by selecting another cell -different from the one that I'm concerned in- before I get the values. But that's not a solution for me
From the comments, you are using a button that doesn't take the focus away from the grid, so it remains in edit mode. Try it like this:
MarksDGV1.EndEdit();
MessageBox.Show(MarksDGV1.Rows[0].Cells[1].Value.ToString());
I need to select multiple rows in my dataGridView and for all selected rows I need to set all ProductsNames [cell 2] to one label.
Here's my code
Bill bill = new Bill();
foreach (DataGridViewCell cell in dataGridView2.SelectedCells)
{
bill.label12.Text = this.dataGridView2.CurrentRow.Cells[2].Value.ToString() + Environment.NewLine + this.dataGridView2.CurrentRow.Cells[2].Value.ToString();
bill.label14.Text = this.dataGridView2.CurrentRow.Cells[8].Value.ToString();
bill.label15.Text = this.dataGridView2.CurrentRow.Cells[6].Value.ToString();
bill.StartPosition = FormStartPosition.CenterScreen;
bill.Show();
}
It is unclear what exactly your requirements are, however from what little is revealed, I am guessing that you want the user to be able to select “multiple rows”, then, through some unknown mechanism these “selected rows” populate several labels. This is about as much as I can decipher here.
Since it appears you are looking for rows, with full row select mode set, I am guessing it may be easier to loop through the SelectedRows as opposed to SelectedCells.
The thing to keep in mind for all of the “Selected” cells/rows/columns methods is that the collections are stored from a NEWEST to OLDEST order. In other words, the SelectedRows collection is a stack, each time the user adds to the selection, the newly selected cells will go to the top of the SelectedRows collection and everything else moves down. This enables you to track the order the user selected the cells if necessary.
Given this, from your question it appears you need at least two rows. Since multi-select is true, you must assume that the user may have more than two rows selected or possibly only one row selected. This can be checked by looking at the number of SelectedRows.Count. It is not clear what you would do here if there is more than two rows selected. I assume possibly use the first two selected rows, or the last two selected rows.
Below is an example that demonstrates the SelectedRows collection. The DataGridViews SelectionMode should be set to FullRowSelect. A button is used to loop through all the selected rows and outputs some of the values into a text box. I Hope this makes sense.
private void Form1_Load(object sender, EventArgs e) {
SetColumns();
FillGrid();
}
private void SetColumns() {
for (int i = 0; i < 9; i++) {
dataGridView1.Columns.Add("Col" + i, "Col" + i);
}
}
private void FillGrid() {
int newRowIndex = 0;
for (int row = 0; row < 10; row++) {
newRowIndex = dataGridView1.Rows.Add();
for (int col = 0; col < dataGridView1.Columns.Count; col++) {
dataGridView1.Rows[newRowIndex].Cells[col].Value = "R" + row + "C" + col;
}
}
}
private void button1_Click(object sender, EventArgs e) {
foreach (DataGridViewRow curRow in dataGridView1.SelectedRows) {
textBox1.Text += "--- New row ---" + Environment.NewLine;
textBox1.Text += "Label1: " + curRow.Cells["Col2"].Value.ToString() + Environment.NewLine;
textBox1.Text += "Label2: " + curRow.Cells["Col8"].Value.ToString() + Environment.NewLine;
textBox1.Text += "Label3: " + curRow.Cells["Col6"].Value.ToString() + Environment.NewLine;
}
}
I want to select column from data grid view.
Then, I want to add data in cells number 5 for the selected column.
Below code is not worked.
for (int i = 0; i < dgvSRP.Rows.Count; ++i)
{
dgvSRP.Rows.Add();
DataGridViewRow row = this.dgvSRP.Rows[i];
row.Cells[5].Value =txtJumlahPEsanan.Text;
}
foreach(var dgvRow in dgvSRP.Rows)
{
dgvRow.Cells[4].Value = txtJumlahPEsanan.Text; // The 5th column is index 4. Indexes always start at 0.
}
Here is my code i have select specific column value and assign it to textbox. May this solve your problem. Remember i have two cells rows thats why i kept cell[1] because index start from 0.
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (i == 5)
{
textBox3.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
}
}
Is it possible to set a components visible attribute based on its name?
I have 12 "master" components (comboboxes) if you want to call them that and based on the selection in these I want to display anywhere from 1 to 16 textboxes. These are named in numeric order such as combobox1_textbox_0, combobox1_textbox_1 and so on. What I would like to do ideally is take the index of the combobox and pass it as a parameter to a method that sets the textboxes visible attribute to visible/hidden depending on the index passed into the method.
Is this possible? in pseudocode or what you call it I would like it to work something like this:
private void methodToSetVisibleAttribute(int indexFromMainComboBox)
{
for(int i = 0; i < 15; i++)
{
if(i < index)
{
combobox1_textbox_+i.Visible = true;
}
else
{
combobox1_textbox_+i.Visible = false;
}
}
}
I could do panels or something for the choices but seeing as all the selections from the combobox will use the same textboxes but in different amounts it seems like alot of work to make a panel for every possible selection not to mention difficult to expand the program later on.
Assuming you are using Windows Forms and not WPF, you can use ControlCollection.Find() to find controls by name:
var textBox = this.Controls.Find(string.Format("combobox1_textbox_{0}", i), true).OfType<ComboBox>().FirstOrDefault();
if (textBox != null)
textBox.Visible = (i < index);
else
Debug.Assert(false, "textbox not found"); // Or throw an exception if you prefer.
I'll suggest an alternative to your approach, maybe not quite what you're looking for:
Place your combo boxes in a List<ComboBox> and you can access them by an index number.
List<ComboBox> myCombos = new List<ComboBox>();
for (int i = 0; i < 16; i++)
{
ComboBox cb = new ComboBox();
//do what ever you need to do here. Set its location, add items, etc.
Form1.Controls.Add(cb); //Alternatively add it to another container.
myCombos.Add(cb); //Now it's in a list.
}
Modify them like this:
for(int i = 0; i < 15; i++)
{
if(i < index)
{
myCombos[i].Visible = true;
}
else
{
myCombos[i].Visible = false;
}
}
Or even more succintly:
for(int i = 0; i < 15; i++)
{
myCombos[i].Visible = i < index;
}