Selecting one drop down at a time - c#

i have a gridview and each row has Drop down list, i want if user select value in one drop down then he may not select another drop down but if he selects then it should through error:
Sorry , you can select 1 value at a time
int count = 0;
protected void ddlIsComplaint_SelectedIndexChanged(object sender, EventArgs e) //not used yet
{
count = count + 1;
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
//int ID = Convert.ToInt32(row.Cells[0].Text);
//short IsComplaint = Convert.ToInt16(ddl.SelectedValue);
HiddenFieldIsValidDropDownValue.Value = row.Cells[0].Text;
int RowID = Convert.ToInt16(HiddenFieldIsValidDropDownValue.Value);
if (count >= 1)
{
lblSelectionMessage.InnerText = count.ToString();
}
}

Label1.Text = "yes";
protected void ddlIsComplaint_SelectedIndexChanged(object sender, EventArgs e) //not used yet
{
if(Label1.Text!="No")
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
//int ID = Convert.ToInt32(row.Cells[0].Text);
//short IsComplaint = Convert.ToInt16(ddl.SelectedValue);
HiddenFieldIsValidDropDownValue.Value = row.Cells[0].Text;
int RowID = Convert.ToInt16(HiddenFieldIsValidDropDownValue.Value);
}
else
{
lblSelectionMessage.InnerText = count.ToString();
}
}

Related

How to check if the quantity inserted is greater than the new stock in my Datagridview Cell

I want to create a function that will check if the quantity inserted is greater than the new stock in my Datagridview Cell ["New Stock"] to prevent the data from execution.
double qty,totalprice,discount;
double.TryParse(txtqty.Text, out qty);
double.TryParse(txttotalprice1.Text, out totalprice);
double.TryParse(txtdiscount.Text, out discount);
//Boolean to check if he has row has been
bool Found = false;
if (dataGridView1.Rows.Count > 0)
{
// Check if the product Id exists with the same Price
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToString(row.Cells[0].Value) == cmbproductcode.Text && Convert.ToString(row.Cells[3].Value) == txtprice1.Text)
{
//Update the Quantity of the found row
row.Cells[5].Value = Convert.ToString(qty + Convert.ToInt16(row.Cells[5].Value));
row.Cells[7].Value = Convert.ToString(discount + Convert.ToInt16(row.Cells[7].Value)); //txtqty
row.Cells[8].Value = Convert.ToString(totalprice + Convert.ToInt32(row.Cells[8].Value)); //txttotalprice
Found = true;
getprice();
}
}
if (!Found)
{
//Add the row to grid view
getinfo();
}
}
else
{
//Add the row to grid view for the first time
getinfo();
}
public double prev;
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
string cellValue;
int i = e.RowIndex;
cellValue = dataGridView1.Rows[i].Cells[1].Value.ToString();
prev = Convert.ToDouble(cellValue);
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
string cellValue;
int i = e.RowIndex;
cellValue = dataGridView1.Rows[i].Cells[1].Value.ToString();
double cur = Convert.ToDouble(cellValue);
if(isvalid(prev,cur))
dataGridView1.Rows[i].Cells[1].Value = prev.ToString();
else
dataGridView1.Rows[i].Cells[1].Value = cellValue;
}
private Boolean isvalid(double pr,double cur)
{
return cur > pr ? false : true;
}

How to add a value at the first empty cell in column?

In my application, I have a datagridview. For each column, there is a button. If clicked, I take the column index and now want to add a user enterd value at the first (bottom or top doesn't matter) free cell of that column. If there is no empty cell, I want to create one.
I tried looping through all rows, getting the cell values and checking if they are empty.
string data = "%VALUE%";
int rowIndex = -1;
foreach (DataGridViewRow row in dataGridViewTasks.Rows)
{
data = (string) row.Cells[columnIndex].Value;
if (string.IsNullOrWhiteSpace(data))
{
rowIndex = row.Index;
break;
}
else if (row.Cells[columnIndex].RowIndex == dataGridViewTasks.Rows.Count - 1)
{
rowIndex = dataGridViewTasks.Rows.Add();
break;
}
}
if (string.IsNullOrWhiteSpace(data) && rowIndex > -1)
{
dataGridViewTasks[columnIndex, rowIndex].Value = task.name;
}
Problem with this solution is:
If for example row 1 and 2 of column A are filled, a new value in column B is added in row 3. So that is not what I want.
If I understand you correctly, it`s should work.
Try it:
private void Button1_Click(object sender, EventArgs e)
{
var columnIndex = 1;
var dataGrid = new DataGridView();
var index = GetFirstEmptyCellByColumnIndex(dataGrid, columnIndex);
dataGrid[columnIndex, index].Value = "new value";
}
private int GetFirstEmptyCellByColumnIndex(DataGridView dataGrid, int columnIndex)
{
var index = -1;
foreach (DataGridViewRow row in dataGrid.Rows)
{
if (string.IsNullOrEmpty((string)row.Cells[columnIndex].Value))
{
index = row.Index;
break;
}
}
return index != -1
? index
: dataGrid.Rows.Add();
}

how to get value of datagridview then will be fetch by the another window form

I'm creating an ordering system and I add datagridview to view the data on my database and listview to input the orders, when I double click rows on my datagridview, mealname will be transferred to listview on index 0, the quantity and totalprice is from another windowform but I can't get the original price per piece, It always get 0 value.
here is my code on my datagridview
public void dataGridView1_DoubleClick(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
String mealname = dataGridView1.SelectedRows[0].Cells[1].Value + String.Empty;
String price1 = dataGridView1.SelectedRows[0].Cells[2].Value + String.Empty;
listView1.Items.Add(mealname);
qtyOfOrders orders = new qtyOfOrders();
orders.Show();
}
here is my code for my new windowform when datagridview is doubleclicked
`private void OK_Click(object sender, EventArgs e)
{
cashier c = new cashier();
if (c.dataGridView1.SelectedRows.Count > 0)
{
int row = c.dataGridView1.CurrentCell.RowIndex;
int col = c.dataGridView1.CurrentCell.ColumnIndex;
c.listView1.Items.Add(new ListViewItem(c.dataGridView1.Rows[row].Cells[col].Value.ToString()));
c.qtyOrder.ListView.Items.Add(QTYNumber.Text);
int qtyToInt = Convert.ToInt32(QTYNumber.Text);
int priceToInt = Convert.ToInt32(c.dataGridView1.SelectedRows[0].Cells[2].Value);
int totalPrice = qtyToInt * priceToInt;
c.qtyOrder.ListView.Items[0].SubItems.Add(QTYNumber.Text);
c.price.ListView.Items[0].SubItems.Add(totalPrice.ToString());
}
}`
Doesn't seem hard :)
int row = dataGridView1.CurrentCell.RowIndex; //This gets the row index(or count) of the selected row
int col = dataGridView1.CurrentCell.ColumnIndex; //This gets the column index(or count) of the selected row
form2.Listview1.Items.Add(new ListViewItem(dataGridView1.Rows[row].Cells[col].Value.ToString()); ///Finally inserts the column's data/value in the listview :)
Ow,and about the double click ,make sure to put this code in MouseDoubleClick event :)
A few fixes in your code
int qtyToInt = Convert.ToInt32(QTYNumber.Text);
int priceToInt = Convert.ToInt32(c.dataGridView1.SelectedRows[0].Cells[2].Value);
int totalPrice = qtyToInt * priceToInt;

Getting 2 values of cell in datagridview

I have two datagridviews. datagridview1 and datagridview2. When I add a product from datagridview1 to datagridview2 the quantity of the product in datagridview1 is transferred to datagridview2. Now when I remove a product from datagridview2 I need it to transfer the quantity back to datagridview1.
Here is my code.:
private void btnRemove_Click(object sender, EventArgs e)
{
int ind = findIndexForItem(dgvPOScart.CurrentRow.Cells[0].Value.ToString());
int dgvCARTquantity = Convert.ToInt32(dgvPOScart.CurrentRow.Cells[4].Value.ToString());
int dgvPOSquantity = Convert.ToInt32(dgvPOScart.Rows[ind].Cells[5].Value.ToString());
int dgvnewADDquantity;
dgvnewADDquantity = dgvPOSquantity + dgvCARTquantity;
foreach (DataGridViewRow item in this.dgvPOScart.SelectedRows)
{
dgvPOScart.Rows.RemoveAt(item.Index);
}
}
And the code for the helper:
private int findIndexForItem(string name)
{
int ind = -1;
for (int i = 0; i < dgvPOSproduct.Rows.Count; i++)
{
if (dgvPOSproduct.Rows[i].Equals(name))
{
ind = i;
break;
}
}
return ind;
}
How can I properly call ind? Rows[ind] is wrong because ind is the product ID or value or cell[0] and not row index. Or is there an easier way to do this?
Your code is a little weird, you are foreach-ing the SelectedRows but you're only calculating the new quantity for the current row, why?
Also, you shouldn't look at the products by their name since you have their ID (which is more unique than a name).
In order for this to work, you'll need something like this:
private void btnRemove_Click(object sender, EventArgs e)
{
foreach (var row in dgvPOScart.SelectedRows)
{
// Get the row in dgvProducts and the quantity he'll gain back
var productRow = dgvPOSproduct.Rows[FindRowIndexByID(row.Cells[0].Value)];
int qtyToAdd = Convert.ToInt32(row.Cells[4].Value);
// Add the quantity back
productRow.Cells[5].Value = Convert.ToInt32(productRow.Cells[5].Value) + qtyToAdd;
}
}
private int FindRowIndexByID(string id)
{
for (int i = 0; i < dgvPOSproduct.Rows.Count; i++)
{
if (dgvPOSproduct.Rows[i].Cells[0].Value == id)
{
return i;
}
}
return -1;
}

How do I move items from one gridview to another gridview?

I have two gridviews, and when the user highlights a row on the first gridview and clicks a button, it should move to the second gridview.
When I click on the button that record gets added but it only add the last row I've selected (if I select 20 rows, only the last gets added).
All records that are selected should be moved.
How do I do this in ASP.NET?
private void button1_Click_1(object sender, EventArgs e)
{
DataGridViewRow dr = dataGridView1.SelectedRows[0];
dtItems.Columns.Add("city_ID");
dtItems.Columns.Add("city_Name");
dtItems.Columns.Add("status");
dtItems.Columns.Add("date");
if (dataGridView1.Rows.Count > 1)
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value != null)
{
DataRow row;
row = dtItems.NewRow();
row["city_ID"] = dataGridView1.Rows[i].Cells[1].Value.ToString();
row["city_Name"] = dataGridView1.Rows[i].Cells[2].Value.ToString();
row["status"] = dataGridView1.Rows[i].Cells[3].Value.ToString();
row["date"] = dataGridView1.Rows[i].Cells[4].Value.ToString();
dtItems.Rows.Add(row);
}
}
}
Form2 frm = new Form2(dtItems);
frm.ShowDialog();
}
In Form2 copy this code:
public Form2(DataTable dtIt)
{
dtItems = dtIt;
InitializeComponent();
}
private void AddEmptyRows()
{
for (int i = 1; i <= 5; i++)
{
dataGV.Rows.Add();
}
}
private void Form2_Load(object sender, EventArgs e)
{
AddEmptyRows();
for (int i = 0; i < dtItems.Rows.Count; i++) {
dataGV.Rows[i].Cells[0].Value = dtItems.Rows[i]["city_ID"];
dataGV.Rows[i].Cells[1].Value = dtItems.Rows[i]["city_Name"];
dataGV.Rows[i].Cells[2].Value = dtItems.Rows[i]["status"];
dataGV.Rows[i].Cells[3].Value = dtItems.Rows[i]["date"];
}
dataGV.Enabled = true;
}

Categories

Resources