private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
DataGridView dg = sender as DataGridView;
if (dg != null && dg.SelectedRows.Count > 0)
{
DataGridViewRow row = dg.SelectedRows[0];
if (row != null)
{
textBox1.Text = row.Cells[0].Value.ToString();
textBox2.Text = row.Cells[1].Value.ToString();
textBox3.Text = row.Cells[2].Value.ToString();
comboBox10.Text = row.Cells[3].Value.ToString();
dateTimePicker1.Text = row.Cells[4].Value.ToString();
var buletinID = (int)row.Cells[15].Value;
var dateBuletin = db.Buletin.Where(i => i.ID == buletinID + 1).First();
textBox16.Text = dateBuletin.Seria.ToString();
textBox17.Text = dateBuletin.Numarul.ToString();
textBox18.Text = dateBuletin.Eliberat_de_oficiul.ToString();
dateTimePicker3.Text = dateBuletin.Data_eliberarii.ToString();
var livretID = (int)row.Cells[16].Value;
if (livretID != null)
{
// This is the line that throws.
var dateLivret = db.Livret_militar.Where(i => i.ID == livretID).First();
textBox22.Text = dateLivret.Seria.ToString();
textBox23.Text = dateLivret.Numarul.ToString();
comboBox5.Text = dateLivret.Da_sau_Nu.ToString();
groupBox1.Controls.OfType<RadioButton>().Where(c => c.Text == dateLivret.Evidenta.ToString()).FirstOrDefault().Checked = true;
}
我在textboxes,combobox,datetimepicker,单选按钮组中使用datagridview显示文本。当我选择一行时,文本显示在空框中。