DevExpress Gridview can't show data - c#

I have this code :
private void frmWeld_Load(object sender, EventArgs e)
{
List<Weld> lst = _weldRepository.Get().ToList();
gridControl.DataSource = new BindingList<Weld>(lst) { AllowNew = true };
}
I want to load my data into devExpressGridView
As you can see my data is loaded but the gridview can't show the data and my break point doesn't pass from gridControl.DataSource = new BindingList<Weld>(lst) { AllowNew = true} and my program remains with this state.
Why?
I just add new column to my gridview after this the problems occurreded.
I use entity framework ,when i change the database my application creates a new database using code first and after that my data is lost but the problem that i said is solved.

Since your code is executing in Load event, add ForceInitialize() to your code.
private void frmWeld_Load(object sender, EventArgs e)
{
// your previous code
gridControl.ForceInitialize(); <- add this line
}

Related

Cannot get value from database-bound dropdownlist

I'm using asp.net 4.5 web forms with VS2017 and using dropdownlist to get the values
from the database and try to get the value but it keeps giving me only the top value, so I'm asking for help.
The way I binded the value is this :
protected void Page_Load(object sender, EventArgs e)
{
var db = new dbContext();
var CustItem = db.customer.ToList();
customerDropDownList.DataSource = CustItem;
customerDropDownList.DataTextField = "cust";
customerDropDownList.DataValueField = "cust";
customerDropDownList.DataBind();
}
and it works nicely and gets all the list in customer table and populates the data in cust column in the id : customerDropDownList.
And then I tried to get the value from customerDropDownList by having a testBtn
with testLbl attached and used
protected void testBtn_Click(object sender, EventArgs e)
{
testLbl.Text = customerDropDownList.SelectedValue;
}
and it only selects the top element always.
I suspect this has to do with the lifecycle of asp.net and am studying about it
but cannot find the clear answer to solve this.
Could anyone help me on this?
Probably because you are resetting the dropdown list on each page load. Try only setting the ddlist the first time.
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
var db = new dbContext();
var CustItem = db.customer.ToList();
customerDropDownList.DataSource = CustItem;
customerDropDownList.DataTextField = "cust";
customerDropDownList.DataValueField = "cust";
customerDropDownList.DataBind();
}
}
you are missing Page.IsPostBack in page load event
more on Ispostback here
With your code are you not noticing your dropdown list is doubled on the click ?
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
var db = new dbContext();
var CustItem = db.customer.ToList();
customerDropDownList.DataSource = CustItem;
customerDropDownList.DataTextField = "cust";
customerDropDownList.DataValueField = "cust";
customerDropDownList.DataBind();
}
}
Make sure to load the dropdown only once. your page load will be called on the button click as well.
Keep a break point and play around !! Good luck

How to edit a database from another form c#

I am completely new on Visual C#. I am trying to create a program with a database to register products and costumers. I just followed these 2 walktrhoughs:
Creating a Local Database File in Visual Studio
Connecting to Data in a Local Database File (Windows Forms)
Then I created 3 Forms on my solution:
Form1: splash screen that is hidden after some seconds and opens the Form2.
Form2: With 4 panels, [A] to add new and edit registers for products (I dragged and dropped the table in 'Details' mode), [B] with the DataGridView which cells are configured to read Only = true. The other 2 panels are the same for costumers.
Form3: a Dialog Box that opens when double click a cell from DataGridView. This dialog box shows a product data with just few fields editable.
My problem:
If I edit a field on Form2 [A] and save it I can see the changes on the DataGridView [B] and on the Form3.
If I edit a field on Form3 and save it, then close the dialog box, there is no change on Form2 neither on panel [A] nor [B]. But when I open the Form3 again the edited data is there. And if I edit the same field on [A] that I edited on Form3 and try to save , occurs the following error:
An unhandled exception of type 'System.Data.DBConcurrencyException' occurred in LojaEstiloDesign20150906.exe
Additional information: Concurrency violation: the UpdateCommand affected 0 of the expected 1 records."
This seems to be a very basic problem, but I have no idea how to solve it. I already tried lot of things but none have worked.
Any help will be very appreciated.
Form2:
namespace LojaEstiloDesign20150906
{
public partial class frm_Menu : Form
{
public frm_Menu()
{
InitializeComponent();
}
private void CadProdutosToolStripMenuItem_Click(object sender, EventArgs e)
{
panel_CadastroProdutos.Visible = true;
panel_CadastroProdutos.Dock = DockStyle.Fill;
panel_CadastroClientes.Visible = false;
panel_ConsultaClientes.Visible = false;
panel_ConsultaProdutos.Visible = false;
}
private void CadClientesToolStripMenuItem_Click(object sender, EventArgs e)
{
panel_CadastroClientes.Visible = true;
panel_CadastroClientes.Dock = DockStyle.Fill;
panel_CadastroProdutos.Visible = false;
panel_ConsultaClientes.Visible = false;
panel_ConsultaProdutos.Visible = false;
}
private void ConProdutosToolStripMenuItem1_Click(object sender, EventArgs e)
{
panel_ConsultaProdutos.Visible = true;
panel_ConsultaProdutos.Dock = DockStyle.Fill;
panel_CadastroClientes.Visible = false;
panel_ConsultaClientes.Visible = false;
panel_CadastroProdutos.Visible = false;
}
private void ConClientesToolStripMenuItem1_Click(object sender, EventArgs e)
{
panel_ConsultaClientes.Visible = true;
panel_ConsultaClientes.Dock = DockStyle.Fill;
panel_CadastroClientes.Visible = false;
panel_ConsultaProdutos.Visible = false;
panel_CadastroProdutos.Visible = false;
}
private void frm_Menu_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void produtosBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.produtosBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.lojaEstiloDataSet);
}
private void frm_Menu_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'lojaEstiloDataSet.Clientes' table. You can move, or remove it, as needed.
this.clientesTableAdapter.Fill(this.lojaEstiloDataSet.Clientes);
// TODO: This line of code loads data into the 'lojaEstiloDataSet.Produtos' table. You can move, or remove it, as needed.
this.produtosTableAdapter.Fill(this.lojaEstiloDataSet.Produtos);
}
private void produtosDataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
frm_FichaProdutos frm3 = new frm_FichaProdutos();
frm3.Show();
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
this.Validate();
this.clientesBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.lojaEstiloDataSet);
}
}
}
Form3:
namespace LojaEstiloDesign20150906
{
public partial class frm_FichaProdutos : Form
{
public frm_FichaProdutos()
{
InitializeComponent();
}
private void frm_FichaProdutos_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'lojaEstiloDataSet.Produtos' table. You can move, or remove it, as needed.
this.produtosTableAdapter.Fill(this.lojaEstiloDataSet.Produtos);
// TODO: This line of code loads data into the 'lojaEstiloDataSet.Produtos' table. You can move, or remove it, as needed.
this.produtosTableAdapter.Fill(this.lojaEstiloDataSet.Produtos);
}
public void produtosBindingNavigatorSaveItem_Click_1(object sender, EventArgs e)
{
this.Validate();
this.produtosBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.lojaEstiloDataSet);
}
}
}
ADO.NET uses optimistic concurrency by default. That means that it stores two versions of the data (original and current) and then, when you try to save modifications to existing records, it compares the original version to whats in the database to make sure that they match before saving. The idea is that it prevents two users both retrieving the same data and both trying to save changes and having the second save overwrite the changes made in the first.
In your case, it's not two different users with two different application instances though. It's two forms within the same application instance. Your are retrieving the data in the first form, retrieving the data in the second form, saving changes from the second form and then trying to save changes from the first form. The original version of the data in the first form does not match what is in the database and so a concurrency violation occurs.
You have two choices as to how to proceed. In a valid concurrency violation, i.e. two different users, you would catch the exception and then decide what to do, which would likely be to get the data again and have the user re-edit or possibly merge their changes. That's not the way to go in your case. You should either throw away the data that you have in the first form and get it again when the second form closes or else don't get the same data twice in the first place.
In that second option, instead of the second form getting its own data, the first form could pass it the data that it already has. Because there is only one copy of the data, it will always remain in sync with the database.

how to update database from datagridview

I've checked the option Enable Editing in the datagridview
I've a dataset model generated from my database i'm trying to use update method but its not taking the right parameters
on load even it is fetching and showing into datagridview.
private void FrmSession_Load(object sender, EventArgs e)
{
tblSessionTableAdapter tblSession = new tblSessionTableAdapter();
dataGridView1.DataSource = tblSession.spGetSessionRecord();
}
but on Button click even its not updating from datagridview am i missing something?
private void btnAdd_Click(object sender, EventArgs e)
{
FMSDataSet ds = new FMSDataSet(); //Object of automatically generated model
tblSessionTableAdapter tblSession = new tblSessionTableAdapter();
tblSession.Update(ds); // i put ds here because on update's third constructor it requires dataset
}
The problem is that a new DataSet is created in click method. The new DataSet doesn't contains any modification because it's freshly created.
Modifications are contains in the DataSet/DataTable that is the DataSource of the GridView.
tblSession.Update([Put the same DataSet/DataTable as in load method here]);
If you want your data set to have state then you need to make your data set a member variable so you can keep the current state and update it.
FMSDataSet ds;
Constructor()
{
ds = new FMSDataSet();
}
//Then access it in your click event.
private void btnAdd_Click(object sender, EventArgs e)
{
tblSessionTableAdapter tblSession = new tblSessionTableAdapter();
tblSession.Update(this.ds); // update persistent data set
}

How to display a datatable in the view?

I have a visual studio windows from called managerForm. Clicking a button on the manager form will bring up a filter form. Also, I have a separate dataGridView Form.
By clicking "SELECT" button on the filter form, I will get a SQL dataTable. I want to display the table on the dataGridView Form.
I don't know how to connect them together. Any help?
dg.myDatagridView.DataSource = filter.returnedDataList;
In this code, I cannot get the returnedDataList since filter closed.
private void btnDisplayFilter_Click(object sender, EventArgs e)
{
filterForm filter = new filterForm();
filter.ShowDialog(this);
displayGridViewControl dg = new displayGridViewControl();
dg.myDatagridView.DataSource = filter.returnedDataList;
displayGridView.ShowDialog();
}
You could try with using statement
private void btnDisplayFilter_Click(object sender, EventArgs e)
{
using(filterForm filter = new filterForm())
{
if(filter.ShowDialog(this) == DialogResult.OK)
{
displayGridViewControl dg = new displayGridViewControl();
dg.myDatagridView.DataSource = filter.returnedDataList;
displayGridView.ShowDialog();
}
}
}
This will keep the instance of filterForm still available and you could get the public properties of this instance until your code reaches the closing brace of the using statement

ASP.NET gridview binding doesn't work / control doesn't show up

another beginner problem. Why isn't the following code with an asp.net page not working?
protected void Page_Load(object sender, EventArgs e)
{
List<string> list = new List<string>();
list.Add("Teststring");
this.GridView.DataSource = list;
}
GridView is the GridView control on that asp page. However, no grid shows up at all. It's both enabled and visible. Plus when I debug, the GridView.Rows.Count is 0. I always assumed you can just add generic Lists and all classes implementing IList as a DataSource, and the gridview will then display the content automatically? Or is the reason here that it's been done in the page_load event handler. and if, how can I fill a grid without any user interaction at startup?
Thanks again.
You should call DataBind().
You forgot to call the GridView's .DataBind() method. This is what will link the control to its data source and load the results.
Example:
protected void Page_Load(object sender, EventArgs e)
{
List<string> list = new List<string>();
list.Add("Teststring");
this.GridView.DataSource = list;
this.GridView.DataBind();
}
Unlike in winforms, for ASP developement you need to specifically call GridView.DataBind();. I would also break out that code into a separate method and wrap the initial call into a check for postback. That will save you some headaches down the road.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostback)
{
List<string> list = new List<string>();
list.Add("Teststring");
bindMydatagrid(list);
}
}
protected void bindMydatagrid(List<string> list)
{
gv.DataSource = list;
gv.DataBind();
}

Categories

Resources