I'm trying to first add textbox values to add listed by listbox. after that listbox items adding to datagridview. Its fine. i did this. But i'm facing problem is first time I added listbox lists to datagridview. but second time when I add diffrent data to datagridview, first time added data gone. new data only availbale. Anyone know solutuion.? Because i'm trying to check datagridview rows count, after add, But its not working properley. anyone please...
This is my Interface. when I clicked Serial no generate button, Textboxes with Combobox value adding to listboxes. after when I export to datasheet button clicked, adding datas to datagridview. but again I change the list datagridview old data olso change. please help...
This is code currently.......
//Function of DataLoad Listboxes to Datagridview---------->
public void Data_ID_Load()
{
string cmbItemName = cmbitemname.SelectedItem.ToString();
string cmbItemID = txtitemsku.Text;
DataTable dt = new DataTable();
dt.Columns.Add("Label ID", typeof(string));//column 0
dt.Columns.Add("Serial Number", typeof(string));//column 1
dt.Columns.Add("Item Name", typeof(string));//column 2
dt.Columns.Add("Item ID", typeof(string));//column 3
for (int i = 0; i < lstlabelid.Items.Count; i++)
{
//dt.Rows.Add(lstlabelid.Items[i].ToString(), lstserialno.Items[i].ToString(), cmbItemName, cmbItemID);
dt.Rows.Add(new Object[] { lstlabelid.Items[i].ToString(), lstserialno.Items[i].ToString(), cmbItemName, cmbItemID });
}
gridview.DataSource = dt;
}
private void btnexport_Click(object sender, EventArgs e)
{
try
{
if (lbllabelid.Text == "" || lblserialno.Text == "")
{
MetroMessageBox.Show(this, "Please Make First Label ID and Serial No", "QR Print Submission Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
Data_ID_Load();
}
}
catch (Exception ex)
{
MetroMessageBox.Show(this, ex.Message, "Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
1条答案
按热度按时间qxgroojn1#
In your sample code, there is only one gridview.DataSource = dt in the load event; no other data input is found.
You should keep data in dt while adding new data to dt.
To update the data source of datagridview, you need to set it to null first, and then reproduce the binding.
Here's an example of what I wrote:
In fact, databinding in wpf is easier to use.