![](/img/trans.png)
[英]C# show data between two dates in Datagridview. Dates from DatetimePicker or Textbox
[英]How to remove an item from button click in datagridview. And display sum in the textbox
所以我有一個從共享 class 綁定到Bindinglist
的 datagridview。 我在 datagridview 中添加了一個列按鈕。 該程序沒有編碼錯誤,可以運行。 但是當我點擊刪除按鈕時,它給出了錯誤提示:
Current item cannot be removed from the list because there is no current item.
我還有一個文本框,它應該顯示數據網格中的價格總和。 但它不起作用。
我嘗試了什么:
包含datagridview
的表單
public partial class ShoppingCart : UserControl
{
public ShoppingCart()
{
InitializeComponent();
cartlist.DataSource = ProductData.Items;
}
remove items
的按鈕: [已解決]
private void cartlist_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//Remove Row on Remove Click
if (cartlist.Columns[e.ColumnIndex].Name == "Remove")
{
//Pops up Yes/No Message Box
if (MessageBox.Show("Are you sure you want to remove this item?", "Remove Item?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
productBindingSource.RemoveCurrent();//Remove Row if Yes is clicked
}
}
以及應該顯示數據網格中總價格的textbox
: [已解決]
private void ShoppingCart_Load(object sender, EventArgs e)
{
//Show Total Price in the textbox
TotalBox.Text = (from DataGridViewRow row in cartlist.Rows
where row.Cells[2].FormattedValue.ToString() != string.Empty
select Convert.ToInt32(row.Cells[2].FormattedValue)).Sum().ToString();
}
這就是我的datagrid
所綁定的:
public class Product
{
private string item;
private decimal qty, price;
public string Item
{
get => item;
set { item = value; OnPropertyChanged(); }
}
public decimal Quantity
{
get => qty;
set { qty = value; OnPropertyChanged(); }
}
public decimal Price
{
get => price;
set { price = value; OnPropertyChanged(); }
}
}
並在其中:
public static class ProductData
{
public static BindingList<Product> Items { get; set; } = new BindingList<Product>();
}
最后是 output:
它看起來如何
編輯:我讓刪除按鈕正常工作。 我只是更改了productBindingSource.RemoveCurrent();
to cartlist.Rows.RemoveAt(cartlist.CurrentRow.Index);
解決方案。
private void cartlist_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//Remove Row on Remove Click
if (cartlist.Columns[e.ColumnIndex].Name == "Remove")
{
//Pops up Yes/No Message Box
if (MessageBox.Show("Are you sure you want to remove this item?", "Remove Item?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
cartlist.Rows.RemoveAt(cartlist.CurrentRow.Index);//Correction Here
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
_id = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
string _name = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
string _lname = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
string _sex = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
string _age = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
if (e.ColumnIndex == dataGridView1.Columns["delete"].Index && e.RowIndex >= 0)
{
if(groupBox1.Visible==true)
{
groupBox1.Visible = false;
}
_id = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
DialogResult result = MessageBox.Show("Do You Want to delete?", "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result.Equals(DialogResult.OK) && service.deleterecord(_id, _name, _lname, _age, _sex))
{
MessageBox.Show("deleted record");
this.Close();
}
else
{
MessageBox.Show("not deleted");
}
}
if (e.ColumnIndex == dataGridView1.Columns["edit"].Index && e.RowIndex >= 0)
{
groupBox1.Visible = true;
firstname.Text = _name;
lastname.Text = _lname;
gender.Text = _sex;
age.Text = _age;
}
}**Please try this will get the index of cell and delete it or edit just for additional**
您已將ProductData.Items
直接綁定到DataGridView
。
然后在任何地方使用這個集合。
private void Cartlist_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (cartlist.Columns[e.ColumnIndex].Name == "Remove")
{
//if (MessageBox.Show ...
ProductData.Items.RemoveAt(e.RowIndex);
}
}
計算並顯示總數。
private void ShoppingCart_Load(object sender, EventArgs e)
{
TotalBox.Text = ProductData.Items.Sum(x => x.Price).ToString();
}
您可能需要顯示 Total 不斷更新。
訂閱 BindingList 事件。
ProductData.Items.ListChanged += Items_ListChanged;
這里的代碼與 Load 事件中的代碼相同。
private void Items_ListChanged(object sender, ListChangedEventArgs e)
{
TotalBox.Text = ProductData.Items.Sum(x => x.Price).ToString();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.