How can I change this code correctly? - c#

I Made a datagrid with a button. When I click the button it should load data from the database into the grid.
This works. I linked foreign keys to this datatable, and they also load in the datagrid, but they get displayed as dbname.tablename. I want this to display as their name.
How can I do this?
DataClasses1DataContext db = new DataClasses1DataContext();
reservering deResv = new reservering();
chalet hetHuis = new chalet();
klant deKlant = new klant();
public testpanel()
{
InitializeComponent();
}
private void btnTest_Click(object sender, RoutedEventArgs e)
{
var listResv = db.reserverings.OrderBy(x => x.KlantId).ToList(); //
dgTest.ItemsSource = listResv;
}
Let me know if you need anymore info.

Related

Empty datagridview when passed from anothe form

I am trying to pass my datagridview data in first form to another datagridview in second form. However, when I load the second form, the datagridview is empty. Below is my code:
First Form
public Newdatagridd(List<Newclass> records)
{
InitializeComponent();
Records = records;
foreach (var r in Records)
{
String[] row = { r.column1, r.column2, r.column3, r.column4};
dataGridView1.Rows.Add(row);
}
}
//copy data to second form
private void button1_Click(object sender, EventArgs e)
{
pressed = true;
Newdatagrid ndg = new Newdatagrid(dataGridView1.DataSource);
ndg.Show();
}
Second Form
public Newdatagrid(object dataSource)
{
InitializeComponent();
dataGridView2.DataSource = dataSource;
}
There is no data source for datagridView1 because you manually add rows here.
You need to use something like this
var source = new BindingSource();
source.DataSource = Records;
dataGridView1.DataSource = source;
If you are using VS, then there is a simple way to select the datasource from the object in design mode: Select right top coner DataGrid, than select Chose DataSource, select in Combo "Add Project data source" -> Object -> Next and than chose your object class. After it - use code before for binding

How to make combobox select an item from the model?

I am trying to do the update, In my update form there is combobox whose items comes from the model which is filled from the database.
Now what i am trying to do is that it selects the item automatically which is coming from the database of a specific record.
below is the code
public partial class UpdateCompanyForm : Form
{
CompanyModel c = new CompanyModel();
public UpdateCompanyForm(CompanyModel company)
{
InitializeComponent();
c = company;
}
private void UpdateCompanyForm_Load(object sender, EventArgs e)
{
CompanyNameEnValue.Text = c.CompanyNameEn;
SectorComboBox.DataSource = GlobalConfig.Connections.GetAll_Sector();
SectorComboBox.SelectedItem = c.Sector;
SectorComboBox.DisplayMember = "Name";
CategoryComboBox.DataSource = GlobalConfig.Connections.GetAll_Category();
CategoryComboBox.SelectedItem = c.Category;
CategoryComboBox.DisplayMember = "CategoryName";
PhoneNumberValue.Text = c.Contacts.PhoneNumber;
}
}
I have 2 items in the SectorCombobox and 2 items in categorycombobox.
It is filled with by the database but i want it select a specific item.
You all know how update works.

Don't display items to combobox from Entity Framework

I have defined a model in Entity Framework. Now I want to fill out the combobox from this model. But I was unsuccessful.
private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
{
using (SamenEntities c = new SamenEntities())
{
comboBox6.DataSource = c.sabt_como_tahsili.ToList();
comboBox6.ValueMember = "id_vaziat_tahsili";
comboBox6.DisplayMember = "name_vaziat_tahsili";
}
}
No data is displayed in the comboBox
You should fill your combo box when you initialize your form. Or maybe create a RefreshDataSources function which will reload every data set on your form, this combo box included. Like this maybe?
private void RefreshDataSources()
{
using (SamenEntities c = new SamenEntities())
{
#region combobox
comboBox6.DataSource = c.sabt_como_tahsili.ToList();
comboBox6.ValueMember = "id_vaziat_tahsili";
comboBox6.DisplayMember = "name_vaziat_tahsili";
#endregion
// place other controls here
}
}
But doing that in the event SelectedIndexChanged is not the best choice, even when it would work.
EDIT
Just checked the behavior of a usual microsoft combobox item.
The SelectedIndexChanged won't be launched if there is no items inside it, so no chance to execute your code.
public Form1()
{
InitializeComponent();
using (SamenEntities c = new SamenEntities())
{
comboBox6.DataSource = c.sabt_como_tahsili.ToList();
comboBox6.ValueMember = "id_vaziat_tahsili";
comboBox6.DisplayMember = "name_vaziat_tahsili";
}
}

ComboBox behaving in a not expected way

I have two comboboxes which I populate using my GetAllCities() method in the CtrlMap.
My idea is, whenever I select another city on the ddFrom it should databind all the cities to ddTo (and later on remove the exact same selected, so user won't be able to select same city as point From and To).
However, Whenever I select something on ddFrom, ddTo populates (as it should), but SelectedIndex gets the same as the ddFrom. Same goes in the opposite way. If I select a city, lets say New York on ddTo it is also selected on ddFrom.
In the GUINewBooking.Designer.cs there's only this event handler registered: this.ddFrom.SelectedIndexChanged += new System.EventHandler(this.ddFrom_SelectedIndexChanged);
ddTo has no event handler registered. Any ideas?
public partial class GUINewBooking : Form
{
private CtrlMap ctrlMap;
public GUINewBooking()
{
InitializeComponent();
ctrlMap = new CtrlMap();
ddFrom.DataSource = ctrlMap.GetAllCities();
ddFrom.DisplayMember = "name";
}
private void ddFrom_SelectedIndexChanged(object sender, EventArgs e)
{
ddTo.DataSource = ctrlMap.GetAllCities();
ddTo.DisplayMember = "name";
}
}
I believe it's because you are using the same data source. You might need to
private void ddFrom_SelectedIndexChanged(object sender, EventArgs e)
{
CtrlMap ctrlMapTo = new CtrlMap();
ddTo.DataSource = ctrlMap2.GetAllCities();
ddTo.DisplayMember = "name";
}
The answer can be found Strange behavior of Windows Forms combobox control
Each combobox DataSource property should be assigned to a different BindingSource object.
Example:
cmbDataType1.DataSource = new BindingSource(datasource, "");
cmbDataType2.DataSource = new BindingSource(datasource, "");
Or in my particular case:
ddFrom.DataSource = new BindingSource(ctrlMap.GetAllCities(), "");
ddTo.DataSource = new BindingSource(ctrlMap.GetAllCities(), "");

DataGrid.Items filled with Null data

I'm trying to fill a asp.DataGrid with data from a DB2 database. The problem I'm getting is that the data is coming back null. The weird thing is, the records themselves (seem to) load from the database, as when I step through the code while debugging, the DataGrid.Items.Count = the number of records I have in the database itself.
Additionally, while troubleshooting, I've added an asp.Label that's initially hidden to display the number of records found in the DataGrid itself, and each time it displays the correct number of records.
Here's my code:
protected void Page_Load(object sender, EventArgs e)
{
dta_grd = new DataGrid();
dta_grd = Ex_DLL.GetData("select * from tstint/m02");
Lbl_Dsply.Visible = true;
if (Supp_Data.Items.Count == 0)
{
Lbl_Dsply.Text = "No Records Found!";
}
else
{
Lbl_Dsply.Text += dta_grd.Items.Count.ToString();
}
}
Ex_DLL is simply the name of a library that does all the connections to the Database itself.
The Code for Ex_DLL.GetData():
public static DataGrid GetData(string str_sql)
{
//EX_Global.DB2_Conn("DEV") is just an internal connection function that houses
//the connection settings.
iDB2Connection db2_conn = EX_Global.DB2_Conn("DEV");
iDB2Command db2_cmd = null;
iDB2DataAdapter db2_adpt = null;
DataSet dta_ds = new DataSet();
DataGrid ret_val = new DataGrid();
try
{
if (db2_conn.State != System.Data.ConnectionState.Open) { db2_conn.Open(); }
db2_cmd = new iDB2Command(str_sql, db2_conn);
db2_adpt = new iDB2DataAdapter(db2_cmd);
db2_adpt.Fill(dta_ds);
ret_val.DataSource = dta_ds;
ret_val.DataBind();
db2_conn.Close();
}
catch (Exception) { }
return ret_val;
}
Now, when I read them individually using idb2DataReader, it's actually reading from the database, but there's just something lost in translation from reading the database to filling the DataGrid itself.
Any ideas?
There are two problems (may be more) I can see:
You instantiate the DataGrid control during page execution but forget to add it to page's Controls collection.
You missed Datasource property and DataBind() method.
In design environment, add a "PlaceHolder" control on a page (.aspx) (Say PlaceHolder1)
protected void Page_Load(object sender, EventArgs e)
{
dta_grd = new DataGrid();
dta_grd.DataSource = Ex_DLL.GetData("select * from tstint/m02");
dta_grd.DataBind(); // this method populates the DataGrid from assigned datasource
PlaceHolder1.Controls.Add(dta_grd);
Lbl_Dsply.Visible = true;
if (Supp_Data.Items.Count == 0)
{
Lbl_Dsply.Text = "No Records Found!";
}
else
{
Lbl_Dsply.Text += dta_grd.Items.Count.ToString();
}
}

Categories

Resources