void FillComboboxCarPlace()
{
try
{
dSet = new DataSet();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
s = "select Move_id , Move_Place from Car_Move_Place";
sCommand = new SqlCommand(s, con);
sdAdapter = new SqlDataAdapter();
sdAdapter.SelectCommand = sCommand;
sdAdapter.Fill(dSet);
DataRow dr = dSet.Tables[0].NewRow();
dr.ItemArray = new object[2] { 0, " ---Select--- " };
dSet.Tables[0].Rows.InsertAt(dr, 0);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
int index = 0;
var cbxMove = row.Cells[index] as DataGridViewComboBoxCell;
ComboMovePlace.ValueMember = "Move_id";
ComboMovePlace.DisplayMember = "Move_Place";
ComboMovePlace.DataSource = dSet.Tables[0];
}
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
catch
{
return;
}
} private void btn_Search_Click(object sender, EventArgs e) { try { dataGridView1.Rows.Clear(); dataGridView1.Refresh(); string s1 = @"SELECT COUNT(*) FROM Move_Transfer WHERE fromDate = @fromDate"; if (con.State == ConnectionState.Closed) { con.Open(); } sCommand = new SqlCommand(s1, con); sCommand.Parameters.AddWithValue("@fromDate", dateTimePicker_form.Text); int records = (int)sCommand.ExecuteScalar(); if (records == 0) { s = " SELECT Place_desc ,Car_id "; s = s + " FROM Cars "; s = s + " INNER JOIN Place ON Cars.Place_id = Place.Place_id "; sCommand = new SqlCommand(s, con); sdAdapter = new SqlDataAdapter(); sdAdapter.SelectCommand = sCommand; dt = new DataTable(); sdAdapter.Fill(dt); foreach (DataRow item in dt.Rows) { int n = dataGridView1.Rows.Add(); dataGridView1.Rows[n].Cells[0].Value = item["Place_desc"].ToString(); dataGridView1.Rows[n].Cells[1].Value = item["Car_id"].ToString(); FillComboboxCarPlace(); } } else { s = " SELECT Place.Place_desc, Move_Transfer.Car_id, Move_Transfer.busy, Move_Transfer.damage, Move_Transfer.withoutDriver, Car_Move_Place.Move_Place "; s = s + " FROM Move_Transfer "; s = s + " INNER JOIN Cars "; s = s + " ON Move_Transfer.Car_id = Cars.Car_id INNER JOIN "; s = s + " Place ON Cars.Place_id = Place.Place_id INNER JOIN "; s = s + " Car_Move_Place ON Move_Transfer.Move_id = Car_Move_Place.Move_id "; s = s + " where fromDate = '" + dateTimePicker_form.Text + "' "; sCommand = new SqlCommand(s, con); sdAdapter = new SqlDataAdapter(); sdAdapter.SelectCommand = sCommand; dt = new DataTable(); sdAdapter.Fill(dt); foreach (DataRow item in dt.Rows) { int n = dataGridView1.Rows.Add(); dataGridView1.Rows[n].Cells[0].Value = item["Place_desc"].ToString(); dataGridView1.Rows[n].Cells[1].Value = item["Car_id"].ToString(); dataGridView1.Rows[n].Cells[2].Value = item["busy"].ToString(); dataGridView1.Rows[n].Cells[3].Value = item["damage"].ToString(); dataGridView1.Rows[n].Cells[4].Value = item["withoutDriver"].ToString(); dataGridView1.Rows[n].Cells[5].Value = item["Move_Place"].ToString(); } FillComboboxCarPlace(); btn_Search.Enabled = false; } if (con.State == ConnectionState.Open) { con.Close(); } } catch { return; } }
Rajeev PunhaniPosted Jul 6, 2016, 11:33 PM
If the above solution is helpful then,accept the answer.
Nilesh SawardekarPosted Jul 6, 2016, 12:19 PM