I have a DataGridView and I need to add custom objects to it. Consider the following code:
DataGridView grid = new DataGridView();
grid.DataSource = objects
Create new Model For List Like ....
var today =DateTime.Today;
DailyEnteryList = _context.Tbl_AllEntriries.Where(x =>
x.CreatedOn.Value.Date == today.Date).
Select(x=> new EntirysModel
{
EntiryNo=Convert.ToInt64(x.EntiryNo),
CustomerName=x.CustomerName,
Date=Convert.ToDateTime(x.Date),
ModelName=x.ModelName,
}).ToList();
My EntirysModel
public class EntirysModel
{
public long EntiryNo { get; set; }
public string CustomerName { get; set; }
public DateTime Date { get; set; }
public string ModelName { get; set; }
}
Then Add Columns in your grid
[1]
: https://i.stack.imgur.com/3dZAl.png
this is work best in my cash THanks !!!
SqlCommand cmd6 = new SqlCommand("SELECT * FROM tblinv WHERE invno='" + textBox5.Text + "'",cn);
SqlDataReader sqlReader6 = cmd6.ExecuteReader();
if (sqlReader6.HasRows)
{
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
dt.Load(sqlReader6);
dt1 = dt.DefaultView.ToTable(true, "ChallanNo", "ProductName", "UoM", "Price", "Qty","Subtotal");
dataGridView2.DataSource = dt1;
}
Whenever I do this I usually make grid.DataSource
the result of a LINQ projection on the objects.
So something like this:
grid.DataSource = objects.Select(o => new
{ Column1 = o.SomeValue, Column2 = o.SomeOtherValue }).ToList();
The nice thing is that you can then set AutoGenerateColumns
to true, which will generate columns based on the properties of the projected objects.
Edit:
The one downside to this approach is that by projecting everything into an anonymous object, you can have problems in situations where you need to access a specific object in a click event, for example.
In this case you may be better off defining an explicit view model and projecting your objects into those. E.g.,
class MyViewModel
{
public int Column1 { get;set; }
public int Column2 { get;set; }
}
grid.DataSource = objects.Select(o => new MyViewModel()
{ Column1 = o.SomeValue, Column2 = o.SomeOtherValue }).ToList();
Edit 2:
MyViewModel
represents all of the columns you want to display in the DataGridView
. The example properties should of course be renamed to suit what you are doing. In general, the point of a ViewModel is to serve as a sort of converter that mediates between the model (in your case your list of objects) and the view.
If you are wanting to retain a reference to the underlying object, the best way might be to supply it via the constructor:
class MyViewModel
{
public int Column1 { get;set; }
public int Column2 { get;set; }
....
private SomeType _obj;
public MyViewModel(SomeType obj)
{
_obj = obj;
}
public SomeType GetModel()
{
return _obj;
}
}
grid.DataSource = objects.Select(o => new MyViewModel(o)
{ Column1 = o.SomeValue, Column2 = o.SomeOtherValue }).ToList();
The reason I have gone for a getter method to retrieve the underlying model object is simply to avoid a column being generated for it.
You can also use Attribute [Browsable(false)] on any property in the underlying Objects, as may be appropriate. This of course would preempt the column from being browse-able some other place, so you might find that undesirable.
You can use databindings with AutoGenerateColumns = false
and using DataPropertyName like this
grid.Columns["Column_name_1"].DataPropertyName = "public_property_1";
grid.Columns["Column_name_2"].DataPropertyName = "public_property_2";
This way only bound columns will be shown in the datagridview, and you can create the columns in the editor if you want. Public properties can be any public attribute within your object.
If you are editing your data from the datagridview, then you should use NotifyPropertyChanged in the set methods. Se my question/answer here where I explain this all the way down.
You can do something like this.
To display only particluar columns in a DataGridView
first you take the data in a DataTable
like this.
String query="Your query to dispplay columns from the database";
SqlCommand cmd=new SqlCommand(query,con); //con is your Connection String
con.Open();
DataTable dt=new DataTable();
SqlDataAdapter da=new SqlDataAdapter(cmd);
da.Fill(dt); //Now this DataTable is having all the columns lets say
/* Now take another temporary DataTable to display only particular columns*/
DataTable tempDT=new DataTable();
tempDT=dt.DefaultView.ToTable(true,"Your column name","your column name");
//Now bind this to DataGridView
grid.DataSource=tempDT;
con.Close();
I know this is a quite long process.Those who go for the performance might not like this. :P
But i think it works fine.