Both of these functions are working fine, one should fill up datagrid with data, the other, should be triggered when i select a row from the first datagrid. But for some reason the second function is causing datagrid to fill just one row of data.
private void fillDataGrid(DataGridView dg, string query) {
OracleCommand cmd = new OracleCommand(query);
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
OracleDataAdapter oda = new OracleDataAdapter(cmd);
DataTable dt = new DataTable();
try {
cmd.ExecuteNonQuery();
oda.Fill(dt);
**dg.DataSource = dt;**
}
catch(Exception e) {
MessageBox.Show(e.ToString());
}
}
This function is called when i load the form, and it is working fine on its own. But when the next function is causing some kind of problem and i don't know why.
private void dataGridView1_SelectionChanged_1(object sender, EventArgs e)
{
String s = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
String upit = "select dn.* from detaljinarudzbe dn, narudzba n where dn.nid = n.nid and n.jmbg = " + s + "";
fillDataGrid(dataGridView2, upit);
}
It is working fine, but i get just one row in first datagridview. And expection says
system.argument out of range exception:must be non negative and less than the size of collection
Caused by the ** line in first method. I don't get why is the event causing this problem. when i use the second function on a button it is working fine.
You can try (I using SQL Server) : attention check SelectedRows[0] by SelectedRows.Count
private void fillDataGrid(DataGridView dg, string query) {
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
SqlDataAdapter oda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
try {
conn.Open();
cmd.ExecuteNonQuery();
oda.Fill(dt);
conn.Close();
dg.DataSource = dt;
}
catch(Exception e) {
MessageBox.Show(e.ToString());
}
finally
{
}
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if(dataGridView1.SelectedRows.Count > 0)
{
String s = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
String upit = "Select * from CARD_NO where CT_ID = " + s + "";
fillDataGrid(dataGridView2, upit);
}
}
private void Form1_Load(object sender, EventArgs e)
{
fillDataGrid(dataGridView1, "Select * from CARD_TYPE");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.