I am using EntityFramework.
The DELETE function will delete the selected customer and refresh the datagridview, but the ADD function did not refresh the datagridview with the new added customer. Any idea?
public CustomerDialog()
{
InitializeComponent();
nw = new northwindEntities();
}
private void CustomerDialog_Load(object sender, EventArgs e)
{
dgvCustomer.DataSource = nw.Customers;
}
private void btnDelete_Click(object sender, EventArgs e)
{
string strSelectedCustomerID = getSelectedCustomerID();
Customer customer = nw.Customers.Where(a => a.CustomerID == strSelectedCustomerID).First();
nw.Customers.DeleteObject(customer);
nw.SaveChanges();
}
//the new customer is persist on the database, but the dgvCustomer is not update.
private void btnAdd_Click(object sender, EventArgs e)
{
Customer newCustomer = new Customer() {
CustomerID = txtCustomerID.Text,
CompanyName = txtCompanyName.Text,
ContactName = txtContactName.Text
};
nw.Customers.AddObject(newCustomer);
nw.SaveChanges();
dgvCustomer.DataSource = nw.Customers ;
dgvCustomer.Refresh();
}
You may try calling BindingSource.ResetBindings() method after you called the SaveChanges() method.
Also, it could help to use a BindingList as the data source (see the article's code example how to use it).
Ugly, but pragmatic approach: dgvCustomer.DataSource = null; dgvCustomer.DataSource = nw.Customers;
As a side note, dgvCustomer.Refresh() will not help you in this case. It does not refresh the data binding, it will cause the control to redraw itself in the UI, which most is likely not what you intended.
Related
When I GridView data Update in other forms then data update but when close this form then gridview data refresh not response automatically in C# win-forms application
Here my Code:
private void button1_Click(object sender, EventArgs e)
{
aCatagory=new tbl_Catagory();
aContext = new RM_Inventory_DataContext();
var product = (from p in aContext.tbl_Catagories
where p.CatagoriesID == Convert.ToDecimal(textBox1.Text)
select p).Single();
product.CatagoriesName = textBox2.Text;
aContext.SubmitChanges();
this.Close();
AddCatagories addCatagories=new AddCatagories();
addCatagories.GridView();
}
public void GridView()
{
gridControl1.DataSource = new RM_PM_Inverntory_Management_System.RM_Inventory_DataContext().tbl_Catagories;
}
You are not binding gridview after assigning the datasource.
public void GridView()
{
gridControl1.DataSource = What ever Data Source You have
gridControl1.DataBind();
}
My problem is that I can get from page_load to show_books function
but I cant get from show_books to book_detail.
Every time I try to fire book_details it gets me to page_load.
my code is this:
protected void Page_Load(object sender, EventArgs e)
{
//Here I create dynamic linkbuttons that calls datashow
lnk_button.Command += new CommandEventHandler(book_show);
}
protected void book_show(object sender, EventArgs e)
{
//Here I show all books from a category
//and I create dynamic linkbuttons that calls book_details
book_button.Command += new CommandEventHandler(book_details);
}
protected void book_details(object sender, EventArgs e)
{
//and Here I show the details of each book
}
I'm sorry if my question is annoyingly newbie
but I would like some help .
I just started to learn asp.net
well i don't understand why you need to do this,but code below works,hope it helps
protected void Page_Load(object sender, EventArgs e)
{
//Here I create dynamic linkbuttons that calls datashow
lnk_button.Command += new CommandEventHandler(this.book_show);
lnk_button.CommandName = "testClick";
lnk_button.CommandArgument = "1";
lnk_button.ID = "11";
lnk_button.Text = "blah";
book_button.Command += new CommandEventHandler(this.book_details);
book_button.CommandName = "testClick2";
book_button.CommandArgument = "2";
book_button.ID = "22";
book_button.Text = "blah2";
}
protected void book_show(object sender, EventArgs e)
{
//Here I show all books from a category
//and I create dynamic linkbuttons that calls book_details
book_button.Command += new CommandEventHandler(this.book_details);
book_button.CommandName = "testClick2";
book_button.CommandArgument = "2";
book_button.ID = "22";
book_button.Text = "blah2";
}
protected void book_details(object sender, EventArgs e)
{
//and Here I show the details of each book
}
public void book_show()
{
book_details() ;
}
public void book_details()
{
}
I am trying to create a dynamic Data List bind with database. I can easily create this but I am not able to make the item Command of this Data List. Please help me. Here is my code below
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
categorybinding();
}
}
public void categorybinding()
{
int totalcate = (from x in ebooks.books_category select x).Count();
var ra = (from x in ebooks.books_category select x);
DataList dl = new DataList();
dl.ItemTemplate = new DatalistLabelColumnBind();
dl.DataSource = ra;
dl.DataBind();
form1.Controls.Add(dl);
dl.ItemCommand += new DataListCommandEventHandler(this.ItemCommandHandler);
}
public void ItemCommandHandler(object sender, DataListCommandEventArgs e)
{
Response.Redirect("NewPage.aspx?"+e.CommandArgument.ToString());
}
//Create a new class implementing ITemplate
public class DatalistLabelColumnBind : ITemplate
{
public DatalistLabelColumnBind()
{
//Add constructor
}
public void InstantiateIn(Control container)
{
LinkButton label1 = new LinkButton();
label1.DataBinding += new EventHandler(this.BindLabelColumn);
container.Controls.Add(label1);
}
public void BindLabelColumn(object sender, EventArgs e)
{
LinkButton lbl = (LinkButton)sender;
DataListItem container = (DataListItem)lbl.NamingContainer ;
String strVals = Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, "books_category1"));
lbl.CommandArgument = Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, "id_books"));
lbl.Text = strVals;
}
}
My Problem :
My Data List easily added on the page but when I click on the Link Button which is added in the Data List is does not Redirect to the NewPage.aspx
Help me out..
I think your Response.Redirect is not resolving to the intended page.
Try:
Response.Redirect("~/NewPage.aspx?"+e.CommandArgument.ToString());
Write categorybinding() function after the if condition in Page_Load. And ItemCommandHandler will definitely work.
I am using DevExpress EntityInstantFeedbackSource as datasource to an XtraGrid control. However I am not using the connection string from the app.config file; rather I am setting the connection string for entity framework at runtime.
The code is given below:
void Form1_Load(object sender, EventArgs e)
{
entityInstantFeedbackSource1.KeyExpression = "Prodid";
entityInstantFeedbackSource1.GetQueryable += entityInstantFeedbackSource1_GetQueryable;
entityInstantFeedbackSource1.DismissQueryable += entityInstantFeedbackSource1_DismissQueryable;
gridControl1.DataSource = null;
gridControl1.DataSource = entityInstantFeedbackSource1;
}
void entityInstantFeedbackSource1_GetQueryable(object sender, GetQueryableEventArgs e)
{
EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();
ecsb.Metadata = #"res://*/Model2.csdl|res://*/Model2.ssdl|res://*/Model2.msl";
ecsb.Provider = #"System.Data.SqlClient";
ecsb.ProviderConnectionString = #"data source=.\sqlexpress;initial catalog=AdventureWorks; integrated security=True;MultipleActiveResultSets=True;App=EntityFramework";
using (var context = new ObjectContext(ecsb.ConnectionString))
{
context.DefaultContainerName = "AdventureWorksEntities";
ObjectSet<Person> query = context.CreateObjectSet<Person>();
var q = from s in query
select s;
e.QueryableSource = q;
e.Tag = context;
}
}
void entityInstantFeedbackSource1_DismissQueryable(object sender, GetQueryableEventArgs e)
{
((ObjectContext)e.Tag).Dispose();
}
The grid is blank. However if I write a foreach loop around 'query' and view the output in Console.WriteLine then I can see the data.
Also if I set e.QueryableSource = q.ToArray().AsQueryable() then I can see data in the grid. But doing this will load all data at one time there by nullifying the benefit of EntityInstantFeedbackSource.
Why there is no data in query? And how to databind ObjectSet to a gridcontrol?
I believe the reason of this issue is that you are disposing the ObjectContext directly in GetQueryable handler rather then to do it in DismissQueryable only. Moreover you can pass the resulting object set directly to e.QuerableSource.
Thus the correct code should looks like this:
void entityInstantFeedbackSource_GetQueryable(object sender, DevExpress.Data.Linq.GetQueryableEventArgs e) {
//... connection initialization ...
var context = new ObjectContext(ecsb.ConnectionString);
ObjectSet<Person> personSet = context.CreateObjectSet<Person>();
e.QueryableSource = personSet;
e.Tag = context;
}
void entityInstantFeedbackSource_DismissQueryable(object sender, DevExpress.Data.Linq.GetQueryableEventArgs e) {
((ObjectContext)e.Tag).Dispose();
}
I am using DevExpress in my winform application, I have a gridview, data entry form, datanavigator, all bound to dataset.
I want to add new record, if using datanavigator "Add" it works good, how to do the same using a "New Record" button?
BindingSource.AddNew()
is not working, it usually does, but with devexpress its not working.
If you want to use binding then use your objects with binding source..
and use the binding list .AddingNew += new AddingNewEventHandler(listOfParts_AddingNew);
event to add new entity object ..
See the example of BindingList on MSDN.
void listOfParts_AddingNew(object sender, AddingNewEventArgs e)
{
e.NewObject = new Part(textBox1.Text, int.Parse(textBox2.Text));
}
DevExpress WinForm Controls works so fast with binding sources as compare to typed datasources etc... YOu can implement bindingSources using these example..
set gridview and the associcated controls datasource to bindsouce that you have created...
process your form with the this MSDN example..
have a look on this code snippet.. may be you will get some idea from this..
private void BindingLIstDemo_Load(object sender, EventArgs e)
{
InitializeListOfEmployees();
BindlstEmp();
listofEmp.AddingNew += new AddingNewEventHandler(listOfEmp_AddingNew);
listofEmp.ListChanged += new ListChangedEventHandler(listofEmp_ListChanged);
}
private void BindlstEmp()
{
lstEmpList.Items.Clear();
lstEmpList.DataSource = listofEmp;
lstEmpList.DisplayMember = "Name";
}
void listofEmp_ListChanged(object sender, ListChangedEventArgs e)
{
MessageBox.Show(e.ListChangedType.ToString());
//throw new NotImplementedException();
}
//declare list of employees
BindingList<Emp> listofEmp;
private void InitializeListOfEmployees()
{
//throw new NotImplementedException();
// Create the new BindingList of Employees.
listofEmp = new BindingList<Emp>();
// Allow new Employee to be added, but not removed once committed.
listofEmp.AllowNew = true;
listofEmp.AllowRemove = true;
// Raise ListChanged events when new Employees are added.
listofEmp.RaiseListChangedEvents = true;
// Do not allow Employee to be edited.
listofEmp.AllowEdit = false;
listofEmp.Add(new Emp(1, "Niranjan", 10000));
listofEmp .Add (new Emp (2,"Jai", 8000));
}
// Create a new Employee from the text in the two text boxes.
void listOfEmp_AddingNew(object sender, AddingNewEventArgs e)
{
e.NewObject = new Emp (Convert.ToInt32(txtId.Text), txtName.Text,Convert.ToInt32(txtSalary.Text));
}
private void btnAdd_Click(object sender, EventArgs e)
{
Emp empItem = listofEmp.AddNew();
txtId.Text = txtName.Text = txtSalary.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
Form1 obj = new Form1();
obj.Show();
}
private void btnDelete_Click(object sender, EventArgs e)
{
var sg = (from sc in listofEmp.ToList<Emp>() where sc.Name == ((Emp)lstEmpList.SelectedValue).Name select sc);
}
private void lstEmpList_SelectedIndexChanged(object sender, EventArgs e)
{
Emp se = listofEmp[lstEmpList.SelectedIndex];
txtId.Text = se.Id.ToString();
txtName.Text = se.Name;
txtSalary.Text = se.Salary.ToString();
}
Here I am using BindingList as datasouce BindingList<Emp> listofEmp; and on the place of grid listing of records are shown in a listbox control.. but all same...try this with your gridview..