How to add new row to unbound devexpress GridControl? - c#

I have create grid controle and in designer I add 4 columns(BS, Repere, Profil, Quantity),and I want to add new row in click of button, so I use this code:
gridView2.AddNewRow();
gridView2.SetRowCellValue(GridControl.NewItemRowHandle, gridView2.Columns["BS"], rowGridView1["BS"]);
gridView2.SetRowCellValue(GridControl.NewItemRowHandle, gridView2.Columns["Repere"], rowGridView1["Repère"]);
gridView2.SetRowCellValue(GridControl.NewItemRowHandle, gridView2.Columns["Profil"], rowGridView1["Profil"]);
gridView2.SetRowCellValue(GridControl.NewItemRowHandle, gridView2.Columns["Quantity"], quantity.EditValue);
but when I run the code nothing happend,my grid control not binding to any type of data ,How do I solve this problem? , Thanks in advance.

Declare a class that will hold the data for a single Row
class GridRow
{
public string BS { get; set;}
public string Repere { get; set; }
public string Profil { get; set; }
public int Quantity { get; set; }
}
then bind the grid's DataSource property to a list of rows like this
this.gridRows = new List<GridRow>();
this.grid.DataSource = this.gridRows;
then to add a new row do this
this.gridRows.Add(new GridRow() { BS = "some value", Repare = "Some value", Quantity = 10});
this.gridView1.RefreshData();

Related

How to set DataGridView column header through a linq query?

I have an sql database for vehicle details. I want to display some fields of the linq query on a datagridview control with different column headers with spaces other than the database field names. I tried it as below but it gives an error. How do I do this?. Thank you guys.
var vlist = from lst in dc.tblVhcleInfos
select new {"Registration Number"= lst.RegNo, "Make and Model"=lst.makeModel, "Year of Manufacture"=lst.YOM};
dataGridView1.DataSource = vlist;
You cannot set up Custom names for Dynamic LINQ Classes, declare class first, add DisplayName attribute to properties, like this
// Define new Class
public class VhcleInfo
{
[DisplayName("Registration Number")
public string RegNo { get; set; }
[DisplayName("Make and Model")
public string MakeModel {get; set; }
[DisplayName("Year of Manufacture")
public int Year { get; set; }
}
// Create IEnum for Defined Class
var vlist = from lst in dc.tblVhcleInfos
select new VhcleInfo() { RegNo = lst.RegNo, MakeModel = lst.makeModel, Year = lst.YOM };
// Set Data Source
dataGridView1.DataSource = vlist;

Binding Complex Data source of a class into Grid View

Here I have these Classes:
public class CustomerDebt
{
public int ID { get; set; }
public string Name { get; set; }
public string Family { get; set; }
public string Remain { get; set; }
public List<Shops> details = new List<Shops>();
}
public class Shops
{
public List<int> ints = new List<int>();
public List<string> strings = new List<string>();
}
I'm making a list of CustomerDebts objects:
List<CustomerDebt> debts = new List<CustomerDebt>();
and Here I will add one item to it:
Shops shop = new Shops();
shop.ints.Add(1);
shop.ints.Add(2);
shop.strings.Add("M");
shop.strings.Add("S");
shop.strings.Add("F");
CustomerDebt d = new CustomerDebt();
d.ID = 12;
d.Name = "Joe";
d.Family = "Steven";
d.Remain = "1000";
d.details.Add(shop);
debts.Add(d);
Now I bind debts list into a grid view:
gridview.DataSource = debts;
It will automatically fill the grid view with only "ID,Name,Family,Remain" properties of CustomerDebt Class. But I need to bind the List property which is a list of another Class into grid view.
I think it needs something like {get; set;} to be shown in grid, But also I do not know If the grid can show a nested class like this.
Hope I'm clear. What should I do to get a clean Grid view with such list of Class objects?
There is no built-in support for Master Details in the DataGridView Control in Windows Forms. But we have the option to use Third Party Libraries Like:
Telerik.
Devexpress.
As for the binding issue, yes you need to have a public property with a public getter:
public List<Shops> details { set; get; } = new List<Shops>();
For prior to C# 6 you can use:
public class CustomerDebt
{
public CustomerDebt()
{
details = new List<Shops>();
}
public List<Shops> details { set; get; }
}
Another way of doing this other than the accepted answer can be like below. Columns can be added manually and then the rows can be filled accordingly.
this.gridview.Columns.Add("ID", "ID");
this.gridview.Columns.Add("Name", "Name");
this.gridview.Columns.Add("Family", "Family");
this.gridview.Columns.Add("Remain", "Remain");
this.gridview.Columns.Add("Details", "Details");
foreach (var debt in debts)
foreach (var detail in debt.details)
this.dataGridView1.Rows.Add(debt.ID, debt.Name, debt.Family, debt.Remain, "Ints:" + string.Join(",", detail.ints) + " Strings:" + string.Join(",", detail.strings));

Change the order of asp:GridView *autogenerated* columns

First let me say that I've seen similar questions on StackOverflow, but none that I've seen deal specifically with auto-generated columns.
I have a asp:GridView that I am binding to an IEnumerable<Data>, where Data can vary depending on runtime input. However, each Data class shares a couple base properties:
public class BaseData
{
public int ID { get; set; }
public string Name { get; set; }
}
public class AddressData : BaseData
{
public string Street1 { get; set; }
public string Street2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; };
}
public class ContactData : BaseData
{
public string Phone { get; set; }
public DateTime LastUpdated { get; set; }
// tons more public properties...
}
// More classes with lots of different public properties...
These different classes are populated by a database query, so I want to auto-generate columns when I place the data in the GridView because of the large number of different fields. However, I want to display the columns that are in the shared base class first. If I just assign the IEnumerable<Data> to the GridView datasource, the base classes properties are added last to the GridView's columns. I tried remedying the situation like this:
// Callback after getting data from database
public void SetGridView<Data>(IEnumerable<Data> rows) where Data : BaseData
{
// Make sure these columns show up first
BoundField ID = new BoundField() { HeaderText = "ID", DataField = "ID" };
BoundField Name = new BoundField() { HeaderText = "Name", DataField = "Name" };
myGridView.Columns.Clear();
myGridView.Columns.Add(ID);
myGridView.Columns.Add(Name);
myGridView.AutoGenerateColumns = true;
myGridView.DataSource = rows;
myGridView.DataBind();
}
However, this just duplicates the columns at the beginning and end, so the header looks like this:
ID Name Street1 Street2 City State ZipCode ID Name
So is there a way for me to move the columns that I know will be there (base class) to the beginning of the GridView's columns while still having the convenience of auto-generating all the other columns?
Since you are forced to use auto-generated columns, I'd just create an intermediate DataTable to bind the GridView to. In that DataTable object, you can add your columns in the order you prefer. The GridView should then bind in that same order.
Edit:
Modify BaseData, add the following:
private DataTable dataForDisplay;
// This would be overridden by all child classes, adding their columns in the order you want
public virtual DataTable GetDataForDisplay()
{
dataForDisplay = new DataTable();
dataForDisplay.Columns.Add("ID");
dataForDisplay.Columns.Add("Name");
// If you want to change the order, just don't call the base.GetDataForDisplay()
dataForDisplay.Rows.AddRange(this.ID, this.Name);
return dataForDisplay;
}
Then all of the other child classes will override GetDataForDisplay().

Getting value and putting value to combobox

I'm newbie with winforms and I'm using a combobox. I like to learn how to load from Database to Controls (Combobox) and how to SAVE values from Control (combobox) to Database field.
Below I need some help on process get from Combobox and put/select value to combobox... But don't know how to do...
Below I get all rows in a list and bind to a combobox. That works fine, but like to do some actions which I'm struggling.
Can someone advice how to do these action?
I'd like to "add on top" of comboboxList an empty value so they can also select an empty value in comboboxlist.
How to select a value in combobox when putting from DB to Control.. I'm doing like this
comboboxAccount.SeletedText = _account.Number, but it's not selecting.
now it's showing "Number" in combobox, but I like to show Number + "-" Description. How to do this? now it's showin e.g. 4000 but I like to show "4000 - Description1"
I like to store the Account.Id into the Database not the Account.Number, but how can I do that because the combobox is showing the Number... so SelectedText is not which I want.
Code:
public class Account
{
public Int32 Id { get; set; } e.g. 1
public string Number{ get; set; } e.g. 4000 (alpha-numeric)
public string Description1 { get; set; } e.g. Customer
public string Description2 {get; set;} e.g. VAT account
public string Language {get; set;} e.g. EN
}
//returns all rows
IList<Account> _account = new List<Account>(Account_repository.GetAll());
comboboxAccount.DataSource = account;
comboboxAccount.DisplayMember = "Number";
comboboxAccount.Add(??); (see point 1)
//saving to database
Client _client = new Client();
_client.Account = comboboxAccount.??; (see point 4)
1) I'd like to "add on top" of comboboxList an empty value so they can
also select an empty value in comboboxlist.
An item can never be added directly to the comobbox items, if the DataSource property is set.
So, the list(datasource) should be updated with empyt/dummy object before setting it as the DataSource.
You might have to find a way to describe that your current object is a dummy object.
Example, you could introduce an EmptyItem property, and handle/filter it before saving:
public class Account
{
public Account(bool isEmptyItem =false)
{
this.EmptyItem = isEmptyItem;
}
public Int32 Id { get; set; }
public string Number { get; set; }
public string Description1 { get; set; }
public string Description2 { get; set; }
public string Language { get; set; }
public bool EmptyItem { get; private set; }
}
IList<Account> _account = new List<Account>();
_account.Add(new Account(true))
_account.AddRange(Account_repository.GetAll());
.
2) *How to select a value in combobox when putting from DB to Control..I'm doing like this comboboxAccount.SeletedText = _account.Number, but it's not selecting.*
Since the comboBox is binded to an object, you cannot set its SelectedText instead you should use SelectedItem
Example://You need to write a logic to find out how to get this item.
this.comboboxAccount.SelectedItem = this.comboboxAccount.Items[1];
3) now it's showing "Number" in combobox, but I like to show Number + "-"
Description. How to do this? now it's showin e.g. 4000 but I like to
show "4000 - Description1"
You might have to expose another propety say DisplayText and bind it to DisplayMember of the combobox.
Example:
public class Account
{
public string Number { get; set; }
public string Description1 { get; set; }
public bool EmptyItem { get; private set; }
public string DisplayText
{
get{return this.Number + "-" + this.Description1}
}
}
this.comboboxAccount.DisplayMember = "DisplayText";
4) I like to store the Account.Id into the Database not the
Account.Number, but how can I do that because the combobox is showing
the Number... so SelectedText is not which I want.
You should use SelectedItem since you have binded an object
var account = this.comboboxAccount.SelectedItem as Account;
var accountID = account.Id;
First, I think you shoul use something like a Dictionary or List> or DataTable.
1) You should insert your empty value in your List and then bind it to your combobox
_account.Insert(0, "Empty");
2) Define your ValueMember also(you shoul have a List or a DataTable):
If you have a DataTabele:
comboboxAccount.ValueMember = "columnID";
comboboxAccount.DisplayMember = "columnValue";
comboboxAccount.DataSource = yourDataTable;
combobox.SelectedIndex = 0; //your empty value
3) You should create another property in your class and use this as DisplayMember;
string NumberDescription{ get; set; };
comboboxAccount.DisplayMember = "NumberDescription";
4) If you want to store the ID of the selected value you should use your previously defined ValueMember(the column ID):
int value = Convert.ToInt32(cbomboboxAccount.SelectedValue);
// if you want to save the text of the value selected in the combobox:
string strValue = comboboxAccount.Text;

How to selectively display and not display column of object in BindingList which is bond to gridcontrol of devexpress winform?

Dear all devexpress winform gurus:)
My way is simple. First, i create a bindinglist:
private BindingList<QuoteOnGrid> quoteOnGrids = new BindingList<QuoteOnGrid>();
Then my object called quote will be added to the BindingList above, the quote have multiple properties and will be displayed on the grid after binding code below:
gridControl1.DataSource = quoteOnGrids;
The picture below show how manually remove column
a busy cat http://i.minus.com/i1JuwLqAdWy2K.jpg
How could I disable the displaying of last three property of these financial quote object without removing these properties?:)
Thanks in advance!
Please use the GridColumn.Visible property:
//...
gridControl1.DataSource = new BindingList<Person>();
gridView1.Columns["ID"].Visible = false;
}
//...
class Person {
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}

Categories

Resources