Unable to show data on data grid view - c#

I have problems showing my data inside the data into the data grid view. Can anyone help to solve because they never prompt me any errors while compiling and there are also data inside the database. What comes out inside the data grid view is only the columns and no data inside.
private void LoadAllEmpShift()
{
using (testEntities Setupctx = new testEntities())
{
var Viewemp = from ES in Setupctx.employeeshifts
join shifthour sh in Setupctx.shifthours on ES.ShiftHourID equals sh.idShiftHours
select new
{
ES.idEmployeeShift,
ShiftHour_Start = sh.shiftTiming_start,
ShiftHour_Stop = sh.shiftTiming_stop,
ES.EmployeeName,
ES.StartTime,
ES.EndTime,
ES.Date
};
dgvShift.DataSource = Viewemp;
}
}
Any help will be greatly appreciated.

After setting the DataSource property, you need to call
dgvShift.DataBind();
Edit:
I believe the above is for a DataGrid / GridView (in case anyone is using those controls).
For DataGridView, you need to have a BindingSource.
Add the BindingSource control to your form, then set the DataSource property of the BindingSource to Viewemp
dgvBindingSource.DataSource = Viewemp;
dgvShift.DataSource = dgvBindingSource;

Related

DevExpress Winforms Report Designer(when program running) Object DataSource bind to table to get dynamic result

im trying to create report with table in winforms application.
I need to use object as datasource (assembly), and when im trying to bind List to table i only getting one row. Im sure i have more items in collection. I creating datasource and map with each field in class.
I tryed everything, and also i cant find any example of this on the devexpress website.
I added Id to the collection, but didn't helped.
Some code:
This how im binding data:
var incidentCardReportAssetsInformationItems = new List<IncidentCardReportAssetsInformationItem>();
foreach (var assetsInformation in assetsAssignedToIncidentWithTimesInformation)
{
var item = new IncidentCardReportAssetsInformationItem
{
AssetName = assetsInformation.Asset?.Name,
AssetOrganization = assetsInformation.Asset?.UserOrganizationUnit?.CodeNumber,
EventStartTime = assetsInformation.EventStartTime.ToString("F", CultureInfo.CurrentCulture),
TypeOfEvent = assetsInformation.TypeOfEvent
};
incidentCardReportAssetsInformationItems.Add(item);
}
In class this is the list which i trying to bind to the table:
public List<IncidentCardReportAssetsInformationItem> IncidentCardReportAssetsInformationItems { get; set; }
You need to put your labels in the 'Detail' area.

Programmatically create a checkbox binding for a WPF form

I need to programmatically create a binding for a checkbox that resides in a WPF form. Because the checkbox is in a user control that gets added to the form multiple times, I'm unsure how to do this. I've created a binding for a DevExpress RichEdit control which worked and then modified that code for the checkbox, but it didn't work.
My code to return the binding is as follows:
private Binding SetIsCorrectBinding(int row)
{
Binding binding = new Binding("DataModel.DetailList[" + row + "].IsCorrect")
{
Path = new PropertyPath("DataModel.DetailList[" + row + "].IsCorrect"),
Mode = BindingMode.TwoWay
};
return binding;
}
The code to implement the binding is as follows:
Binding cbBind = SetIsCorrectBinding(row);
detailRow.IsCorrect_cb.SetBinding(CheckBox.ContentProperty, cbBind);
No matter what I try, the IsCorrect variable is always false.
Any help with this would be greatly appreciated.
Please try the next:
var xBinding = new Binding();
//a real instance of the object where the source property is defined
//it have to be the same instance which is defined in DataModel.DetailList
xBinding.Source = sourceInstance;
xBinding.Path = new PropertyPath("The_source_property_name");
xBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
xBinding.Mode = BindingMode.TwoWay;
//Use this instead the .SetBinding( , ) where the checkbox is the object to binded to
BindingOperations.SetBinding(checkbox, CheckBox.ContentProperty, xBinding);
Please look at the next solution here, I think it can supply an additional information for you.
I will glad to help if you will have the problem with code.
regards,
6 years late, but I had a similar issue. You need to bind to the Checkbox.IsCheckedProperty
detailRow.IsCorrect_cb.SetBinding(CheckBox.IsCheckedProperty, cbBind);
was the fix you needed.

Winforms Databinding Combobox reverting on lostfocus

I have an Employee object that has an EmploymentStatusID (int) field.
I have a combobox that is filled from an Employment Status enum and bound to the field in the Form_Load:
List<LookupListItem> EmpStatuses = new List<LookupListItem>();
foreach (EmploymentStatuses m in Enum.GetValues(typeof(EmploymentStatuses)))
{
EmpStatuses.Add(new LookupListItem((int)m, m.ToString()));
}
cboStatus.DataSource = EmpStatuses; // Enum.GetValues(typeof(CommonLibrary.Lookups.EmploymentStatuses));
cboStatus.ValueMember = "ItemValue";
cboStatus.DisplayMember = "ItemDesc";
cboStatus.DataBindings.Add("SelectedValue", _presenter.SelectedOfficer, "EmploymentStatusID");
When the form comes up the correct value is displayed in the combobox, but if the user changes the value, it is set back when the combobox loses focus!
Text boxes and simple comboboxes (ie ones with a string collection) on the same form are fine.
You can see that I originally tried just using GetValues on the enum, but I changed it to a list to see if that would help. I've tried using a BindingList, I've tried using DataSourceUpdateMode.OnValidation on the binding. I even tried using cboStatus.DataBindings[0].WriteValue on the selectedindexchanged event. No matter what I do, the value changes back to what it was when the form opened! Any ideas?
i modified your code
List<LookupListItem> EmpStatuses = new List<LookupListItem>();
foreach (EmploymentStatuses m in Enum.GetValues(typeof(EmploymentStatuses)))
{
EmpStatuses.Add(new LookupListItem((int)m, m.ToString()));
}
EmpStatuses.Add(new LookupListItem(<selectedValue>, "SomeText")); //<- my modified code
cboStatus.DataSource = EmpStatuses; // Enum.GetValues(typeof(CommonLibrary.Lookups.EmploymentStatuses));
cboStatus.ValueMember = "ItemValue";
cboStatus.DisplayMember = "ItemDesc";
// Remove this part cboStatus.DataBindings.Add("SelectedValue", _presenter.SelectedOfficer, "EmploymentStatusID");
cboStatus.SelectedValue = <selectedValue> //<- my modified code
i hope this will help :)

DataGridView not displaying a row after it is created

I'm using Visual Studio 10 and I just created a Database using SQL Server CE. Within it, I made a table CSLDataTable and that automatically created a CSLDataSet & CSLDataTableTableAdapter.
The three variables were automatically created in my MainWindow.cs class:
cSLDataSet
cSLDataTableTableAdapter
cSLDataTableBindingSource
I have added a DataGridView in my Form called dataGridView and datasource cSLDataTableBindingSource.
In my MainWindow(), I tried adding a row as a test:
public MainWindow()
{
InitializeComponent();
CSLDataSet.CSLDataTableRow row = cSLDataSet.CSLDataTable.NewCSLDataTableRow();
row.File_ = "file";
row.Artist = "artist11";
row.Album = "album";
row.Save_Structure = "save";
row.Sent = false;
row.Error = true;
row.Release_Format = "release";
row.Bit_Rate = "bitrate..";
row.Year = "year";
row.Physical_Format = "format";
row.Bit_Format = "bitformat";
row.File_Path = "File!!path";
row.Site_Origin = "what";
cSLDataSet.CSLDataTable.Rows.Add(row);
cSLDataSet.AcceptChanges();
cSLDataTableTableAdapter.Fill(cSLDataSet.CSLDataTable);
cSLDataTableTableAdapter.Update(cSLDataSet);
dataGridView.Refresh();
dataGridView.Update();
}
In regards to the DataSet methods I tried calling, I had been trying to find a "correct" way to interact with the adapter, dataset, and datatable to successfully show the row, but to no avail.
I'm rather new to using SQL Server CE Database, and read a lot of the MSDN sites & thought I was on the right track, but I've had no luck.
The DataGridView shows the headers correctly, but that new row does not show up.
I've got it to work by calling the TableAdapterManager.UpdateAll(DataSet) method.
http://msdn.microsoft.com/en-us/library/bb384426.aspx

unable to edit DataGridView populated with results of LINQ query

When i use the results of a linq-to-xml query to populate a datagridview, i cannot edit the datagridview. i've tried setting the datagridview's readonly property to false and that doesn't help. I also added an event handler for cellBeginEdit and put a breakpoint there, but it doesn't get hit. Any idea what i'm doing wrong, or if this isn't possible?
public class MergeEntry
{
public string author { get; set; }
public string message { get; set; }
}
...
var query = from entry in xmlDoc.Descendants("entry")
select new MergeEntry
{
author = entry.Element("author").Value,
message = entry.Element("msg").Value,
}
var queryAsList = query.ToList();
myBindingSource.DataSource = queryAsList;
myDataGridView.DataSource = myBindingSource;
Yes, it is possible to bind to a list created from Linq-To-Xml. I tried to reproduce your problem. I did the following:
Created an empty WindForm project (VS 2008 and .Net 3.5)
Added a DataGridView to the Form.
Wired the CellBeginEdit and CellEndEdit.
Placed the following code in the Form's constructor
string testXML =
#"<p><entry>
<author>TestAuthor1</author>
<msg>TestMsg1</msg>
</entry></p>
";
XElement xmlDoc = XElement.Parse(testXML);
var query = from entry in xmlDoc.Descendants("entry")
select new MergeEntry
{
author = entry.Element("author").Value,
message = entry.Element("msg").Value,
}; //You were missing the ";" in your post, I am assuming that was a typo.
//I first binded to a List, that worked fine. I then changed it to use a BindingList
//to support two-way binding.
var queryAsList = new BindingList<MergeEntry>(query.ToList());
bindingSource1.DataSource = queryAsList;
dataGridView1.DataSource = bindingSource1;
When running the WinForm app, an editable grid was displayed and the CellBeginEdit and CellEndEdit events were fired when they should have been. Hopefully trying to reproduce using the above steps will help you locate the issue you are facing.
Since you're using ToList, your DataSource is actually a List<MergeEntry>, so the fact that it comes from a Linq query doesn't change anything. I suspect the ReadOnly property of the columns (not the DGV) is set to true... I see no other reason why the grid wouldn't be editable
This solution may not efficient but it works for me,
I moved all data (which is from LINQ) into a new collection and passed that new collection as datasource to gridview.
Now gridview allow us to edit cells.

Categories