I have a little problem with populating my DevExpress Gridview, I want to have a two level gridview and using SqlCommand. At first I created a Dataset and added two tables and also defined relation for them. But it does not work. Can you help me find my problem?
Here is my code
string owner = "SELECT [OBJECTID],[Name] ,[Family] ,[Father] ,[Dftarche] ,[Birthday] ,[education] ,[home_address] ,[farm_address] ,[ensurance] ,[phone] ,[home_number] ,[owner_id] FROM [dbo].[OWNER]";
string property = "SELECT [number] ,[owner_ID] ,[GPSId] ,[Energy],[corp_type] ,[Pool],[irrigation] ,[variety] ,[trees] ,[utilizat] ,[address] ,[water_hour] ,[w_source] ,[w_inche],[w_dore],[NoeMalekiat],[MotevasetBardasht],[Area] ,[OBJECTID],[Shape] FROM [dbo].[Property] ";
string strConnString = Properties.Settings.Default.land_gisConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
SqlCommand command = new SqlCommand(owner, con);
SqlDataAdapter adapter = new SqlDataAdapter();
System.Data.DataSet dsMain = new System.Data.DataSet();
adapter.SelectCommand = command;
adapter.Fill(dsMain, "First Table");
adapter.SelectCommand.CommandText = property;
adapter.Fill(dsMain, "Second Table");
dsMain.Tables.Add(iFeatureSet.DataTable.Copy());
adapter.Dispose();
command.Dispose();
DataRelation newRelation = new DataRelation("املاک شخصی", dsMain.Tables["First Table"].Columns["owner_id"], dsMain.Tables["Second Table"].Columns["owner_ID"]);
dsMain.Relations.Add(newRelation);
GridAttrebuteTable.DataSource = dsMain.Tables[2];
// gridView5.DataSource = dsMain.Tables[1];
dataGridView1.DataSource = dsMain;
I searched and found this http://msdn.microsoft.com/en-us/library/bh8kx08z.aspx and it seems my code is right but it does not show anything in grids
Thank you very much for your help
I Could figure out how to fix it.It works fine now(Above Code is edited) but now If I add a new DataTable I do not know why it does not work again
You need a new GridView for each detail table. You can't display both a master and detail in the same GridView.
Try this example
Related
I have 2 field First "Name" and second "NIC" the "Name" Is text field and "NIC" Is combobox field.
My query is i want to load data which is already save into SQL and i want to load it again on my grid how i can do that LIKE the below is my sql result
name
nic
FT Muhammad Ejaz
32-3242-23532
DMB Omer
654564
Yasir Wajid
35
HO Zeeshan Hussain
654987987
SC Ameen Ghulam Rasool
64654
BBBT Hafiz Adil
5464
DHA Ghayas
6456
SC Jameel Maseeh
6456
GBB Abdullah Khairat Ali
65456465
DHA Hamid Manzoor
762837
I am new in C# if Any Missing Please ignore
Here is my Code for These two field in C#
string mainconn = ConfigurationManager.ConnectionStrings["MyCONN"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(mainconn);
string sqlquery = "select top 5 Name,nic from salesman order by nic asc";
sqlconn.Open();
SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
SqlDataAdapter sdr = new SqlDataAdapter(sqlcomm);
DataTable dt = new DataTable();
sdr.Fill(dt);
dataGridView1.Columns[1].DataPropertyName = "Name";
sqlconn.Close();
Regards,
dataGridView1.DataSource = yourData
But I would suggest using Entity Framework when working with databases.
https://learn.microsoft.com/en-us/ef/core/
While adding creating GUI in VS2019 you can add TableDataAdapter which will add/show Datagrid showing that tables data. with this code in designer this.data1TableAdapter.Fill(this.test1DataSet.data1);
But problem is if there are any changes in database/table they will not be reflected in that datagrid.
I tried using data1DataGridView.Update(); and data1DataGridView.Refresh(); but it literally does nothing.
than I added my database connection string as datasource.(I know thats stupid attempt, But I tried it anyway.) which obviously didn't worked and erased my whatever(non updated) data showing in datagrid.
So here is my question is there any simple way to update that automatically every time there is change in database/table ?
Disclaimer: I have it working but another way. I will share that too.
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = #"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Documents\test1.mdb";
OleDbCommand comm = new OleDbCommand("SELECT * FROM data1;", conn);
conn.Open();
OleDbDataAdapter dap = new OleDbDataAdapter(comm);
DataTable ds = new DataTable();
dap.Fill(ds);
data1DataGridView.DataSource = ds;
comm.ExecuteNonQuery();
conn.Close();
According to me there are two things you can do.
1- create a Refresh button and at that button call the userdefined function refresh
public void Refresh(object sender, EventArgs e)
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = #"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Documents\test1.mdb";
OleDbCommand comm = new OleDbCommand("SELECT * FROM data1;", conn);
conn.Open();
OleDbDataAdapter dap = new OleDbDataAdapter(comm);
DataTable ds = new DataTable();
dap.Fill(ds);
data1DataGridView.DataSource = ds;
comm.ExecuteNonQuery();
conn.Close();
}
this function rebind data to your new table every time you clicked
2- you can use timer control to refresh your data grid. I am pasting the link how to use timer control in c# you can follow it, and simply paste the above Refresh function definition in timer control. it will refresh your datagrid according to time interval you provide.
I try researching this everywhere but it appears when I run my windows form application and display my database. Each it keeps automatically skipping and adding rows.
Here is a screen shot of my gridview
Here is also my code where it displays the database when I click a button:
try
{
conn = new MySqlConnection();
conn.ConnectionString = connstring;
query = "INSERT INTO schedule(name) VALUES(#namevalue)";
MySqlCommand cmd = new MySqlCommand(query, conn);
cmd.Parameters.AddWithValue("#namevalue", this.nameEmp.Text);
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Connection Success");
myadapt = new MySqlDataAdapter();
string sq = "SELECT * FROM schedule";
myadapt.SelectCommand = new MySqlCommand(sq, conn);
tb = new DataTable();
myadapt.Fill(tb);
BindingSource src = new BindingSource();
src.DataSource = tb;
dataGridView1.DataSource = src;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
Do you mean you want to show those rows which name column is not empty?
Gridview can just show the table you query from the DB,so you can change the SQL code to get the right table,
for example:change SELECT * FROM schedule to SELECT * FROM schedule WHERE schedule(name) IS NOT NULL.
actually, I figured it out. I had in that code above the statement
INSERT twice in my program. I removed it and redid my database on my server and fixed the problem. So sorry for the trouble.
I want to display data into the gridview from the database
But Currently there is no data into the table but it still showing the records from the table.
Below is my code:-
protected void DisplayGrid()
{
OracleCommand cmd = new OracleCommand("Select * from XXACL_PN_FLAT_STATUS_HIS", ObjPriCon);
DataTable dtfillgrid = new DataTable();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(dtfillgrid);
GrdBookingStatus.DataSource = dtfillgrid;
GrdBookingStatus.DataBind();
}
and on page_load
DisplayGrid();
I tried from here its related to Session but I dont have any session right now.
Also I cross checked the database name, Table name and connection string
UPDATE
I tried with Open and close like below but still it didn't worked
try
{
ObjPriCon.Open();
OracleCommand cmd = new OracleCommand("Select * from XXACL_PN_FLAT_STATUS_HIS", ObjPriCon);
DataTable dtfillgrid = new DataTable();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(dtfillgrid);
GrdBookingStatus.DataSource = dtfillgrid;
GrdBookingStatus.DataBind();
}
catch (Exception ex)
{
}
finally
{
ObjPriCon.Close();
}
Finally after a research of many things, I manage to got the solution.
Actually in Toad.
After executing the select query in the Oracle, the next step what I thought of was COMMITING after I executed my select query.
So when I checked after commiting, It worked for me.
I have a table called Departments where the column i wish to populate my combobox with is named DeptId. below is the datatable i created in my DAL class, but i cant for my life figure out how to put these values into deptCombobox in my form1 class. I am trying to attach it to a button named refreshButton_Click, but nothing works no matter what i try
//DAL
public DataTable fillDeptCombo()
{
SqlConnection con = new SqlConnection(#"Data Source=");
con.Open();
string queryText = "SELECT * FROM DEPARTMENT ";
SqlCommand cmd = new SqlCommand(queryText, con);
da = new SqlDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
con.Close();
return dt;
}
//controller
public DataTable fillDeptCombo()
{
return dal.fillDeptCombo();
}
Any help would be greatly appreciated
You have mentioned "form1", I assume it is for windows forms.
In your form1 cs code, this should do it.
DAL dl=new DAL()
Datatable dt=DAL.fillDeptCombo()
combobox.datasource=dt;
combobox.valuemember="deptID" //replace this with your value
combobox.displayMember="DeptName" //replace this with your text