我在datagridview中创建了一个组合框列。问题是我的组合框是空的,空白,当我点击它时不会下拉。当我使用debuger时,每个属性和值都可以,并且项目存在并且链接到组合框。请帮帮我:(
代码:`
public Repair()
{
Main pp = new Main();
InitializeComponent();
this.label4.Text = pp.label3.Text;
SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; ");
maConnexion.Open();
SqlCommand command = maConnexion.CreateCommand();
SqlCommand command1 = maConnexion.CreateCommand();
if (Program.UserType == "admin")
{
command.CommandText = "SELECT Message, FComponent, ReadValue, ValueReference, RepairingTime FROM FailAndPass WHERE FComponent IS NOT NULL";
command1.CommandText = "SELECT Machine, BoardName, BoardNumber FROM FailAndPass WHERE FComponent IS NOT NULL";
}
else
{
command.CommandText = "SELECT Message, FComponent, ReadValue, ValueReference, RepairingTime FROM FailAndPass WHERE ReportingOperator IS NULL AND FComponent IS NOT NULL";
command1.CommandText = "SELECT Machine, BoardName, BoardNumber FROM FailAndPass WHERE ReportingOperator IS NULL AND FComponent IS NOT NULL";
}
SqlDataAdapter sda = new SqlDataAdapter(command);
SqlDataAdapter sda1 = new SqlDataAdapter(command1);
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
sda.Fill(dt);
sda1.Fill(dt1);
DataColumn dcIsDirty = new DataColumn("IsDirty", typeof(bool));
DataColumn dcIsDirty1 = new DataColumn("IsDirty", typeof(bool));
dcIsDirty.DefaultValue = false;
dcIsDirty1.DefaultValue = false;
dataGridView1.DataSource = dt;
dataGridView2.DataSource = dt1;
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
/**ArrayList list1 = new ArrayList(); //{ "C-C", "C-O", "Absence composant", "Mauvaise valeur", "Mauvais sens", "Mauvais composant" };
list1.Add("C-C");
list1.Add("C-O");**/
List<string> list1 = new List<string> { ("C-C"), ("C-O") };
combo.HeaderText = "FaultCodeByOp";
combo.DataPropertyName = "FaultCodeByOp";
combo.DropDownWidth = 120;
combo.FlatStyle = FlatStyle.Flat;
combo.Width = 90;
combo.DataSource = list1;
dataGridView1.Columns.AddRange(combo);
dt.Columns.Add(dcIsDirty);
dt1.Columns.Add(dcIsDirty1);
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AllowUserToOrderColumns = true;
maConnexion.Close();
dataGridView1.Columns[6].Visible = false;
dataGridView2.Columns[3].Visible = false;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for(int i=0;i<4;i++)
{
dataGridView1.Columns[i].ReadOnly = true;
}
}
foreach (DataGridViewRow row in dataGridView2.Rows)
{
for(int i=0;i<3;i++)
{
dataGridView2.Columns[i].ReadOnly = true;
}
}
}
/**private DataTable GetDescriptionTable()
{
DataTable l_dtDescription = new DataTable();
l_dtDescription.Columns.Add("FaultCodeByOp", typeof(string));
l_dtDescription.Rows.Add("C-O");
l_dtDescription.Rows.Add("C-C");
l_dtDescription.Rows.Add("Absence de composant");
l_dtDescription.Rows.Add("Mauvais composant");
l_dtDescription.Rows.Add("Mauvais sens");
l_dtDescription.Rows.Add("Mauvaise valeur");
return l_dtDescription;
}**/
private void textBox1_TextChanged(object sender, EventArgs e)
{
SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; ");
maConnexion.Open();
string Var1 = textBox1.Text;
SqlCommand command = maConnexion.CreateCommand();
SqlCommand command1 = maConnexion.CreateCommand();
if (Program.UserType == "admin")
{
if (textBox1.Text != String.Empty)
{
//command.Parameters.AddWithValue("@BoardName", Var1 + "%");
//command.Parameters.AddWithValue("@Machine", Var1 + "%");
command.Parameters.AddWithValue("@SerialNum", Var1 + "%");
command1.Parameters.AddWithValue("@SerialNum", Var1 + "%");
//command.Parameters.AddWithValue("@FComponent", Var1 + "%");
//command.CommandText = "SELECT * FROM FailAndPass WHERE BoardName LIKE @BoardName OR Machine LIKE @Machine OR SerialNum LIKE @SerialNum OR FComponent LIKE @FComponent";
command.CommandText = "SELECT Message, FComponent, ReadValue, ValueReference, FaultCodeByOp, RepairingTime FROM FailAndPass WHERE SerialNum LIKE @SerialNum AND FComponent IS NOT NULL";
command1.CommandText = "SELECT Machine, BoardName, BoardNumber FROM FailAndPass WHERE SerialNum LIKE @SerialNum And FComponent IS NOT NULL";
}
}
else
{
if (textBox1.Text != String.Empty)
{
//command.Parameters.AddWithValue("@BoardName", Var1 + "%");
//command.Parameters.AddWithValue("@Machine", Var1 + "%");
command.Parameters.AddWithValue("@SerialNum", Var1 + "%");
command1.Parameters.AddWithValue("@SerialNum", Var1 + "%");
//command.Parameters.AddWithValue("@FComponent", Var1 + "%");
//command.CommandText = "SELECT * FROM FailOnly WHERE (BoardName LIKE @BoardName OR Machine LIKE @Machine OR SerialNum LIKE @SerialNum OR FComponent LIKE @FComponent) AND ReportingOperator IS NULL ";
command.CommandText = "SELECT Message, FComponent, ReadValue, ValueReference, FaultCodeByOp, RepairingTime FROM FailAndPass WHERE (SerialNum LIKE @SerialNum) AND ReportingOperator IS NULL AND FComponent IS NOT NULL ";
command1.CommandText = "SELECT DISTINCT Machine, BoardName, BoardNumber FROM FailAndPass WHERE (SerialNum LIKE @SerialNum) AND ReportingOperator IS NULL AND FComponent IS NOT NULL";
}
}
if (!string.IsNullOrWhiteSpace(textBox1.Text))
{
SqlDataAdapter sda = new SqlDataAdapter(command);
SqlDataAdapter sda1 = new SqlDataAdapter(command1);
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
sda.Fill(dt);
sda1.Fill(dt1);
DataColumn dcIsDirty = new DataColumn("IsDirty", typeof(bool));
DataColumn dcIsDirty1 = new DataColumn("IsDirty", typeof(bool));
dcIsDirty.DefaultValue = false;
dcIsDirty1.DefaultValue = false;
dt.Columns.Add(dcIsDirty);
dt1.Columns.Add(dcIsDirty1);
dataGridView1.DataSource = dt;
dataGridView2.DataSource = dt1;
maConnexion.Close();
dataGridView1.Columns[6].Visible = false;
dataGridView2.Columns[3].Visible = false;
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.IsCurrentRowDirty)
{
dataGridView1.Rows[e.RowIndex].Cells[6].Value = true;
}
}
private void metroButton1_Click(object sender, EventArgs e)
{
SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; ");
maConnexion.Open();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if ((row.Cells[6].Value != null) && (bool)row.Cells[6].Value)
{
SqlCommand command = maConnexion.CreateCommand();
command = new SqlCommand("update FailAndPass set FaultCodeByOp=@Fault, RepairingDate=@RD, RepairingTime = @RT, ReportingOperator=@RO WHERE SerialNum=@Serial", maConnexion);
command.Parameters.AddWithValue("@Fault", row.Cells[4].Value != null ? row.Cells[4].Value : DBNull.Value);
command.Parameters.AddWithValue("@RD", DateTime.Today.ToString("d"));
command.Parameters.AddWithValue("@RT", row.Cells[5].Value != null ? row.Cells[5].Value : DBNull.Value);
command.Parameters.AddWithValue("@RO", this.label4.Text);
command.Parameters.AddWithValue("@Serial", this.textBox1.Text);
command.ExecuteNonQuery();
}
}
maConnexion.Close();
this.Hide();
Repair rep = new Repair();
rep.Show();
}
private void metroButton2_Click(object sender, EventArgs e)
{
this.Hide();
Main ff = new Main();
ff.Show();
}
/**private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex > -1)
{
// Bind grid cell with combobox and than bind combobox with datasource.
DataGridViewComboBoxCell l_objGridDropbox = new DataGridViewComboBoxCell();
// Check the column cell, in which it click.
if (dataGridView1.Columns[e.ColumnIndex].Name.Contains("FaultCodeByOp"))
{
// On click of datagridview cell, attched combobox with this click cell of datagridview
dataGridView1[e.ColumnIndex, e.RowIndex] = l_objGridDropbox;
l_objGridDropbox.DataSource = GetDescriptionTable(); // Bind combobox with datasource.
l_objGridDropbox.ValueMember = "FaultCodeByOp";
l_objGridDropbox.DisplayMember = "FaultCodeByOp";
}
}
}**/
}`
答案 0 :(得分:1)
似乎将DataGridViewComboBoxColumn
设置为ReadOnly
会在循环中引发此错误:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i = 0; i < 4; i++)
{
dataGridView1.Columns[i].ReadOnly = true;
}
}
我试图找出原因.....现在避免使用简单的ReadOnly
子句将其设置为if
:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i = 0; i < 4; i++)
{
if (!(dataGridView1.Columns[i] is DataGridViewComboBoxColumn))
{
dataGridView1.Columns[i].ReadOnly = true;
}
}
}
修改强>
问题似乎是dataGridView1.Columns[0]
。此列不希望使用值设置或将其设置为ReadOnly
。它似乎也没有任何名称(测试它),这是非常奇怪的。尽管如此,如果您在1
开始循环,则错误将消失,即使您的DataGridViewComboBoxColumn
设置为ReadOnly = true
而没有if
子句,也会出错:
for (int i = 1; i < 4; i++)
{
dataGridView1.Columns[i].ReadOnly = true;
}
答案 1 :(得分:0)
好的伙计们。我做了一个新的测试。
这是我的测试代码:
combo.DataSource = list1;
if(combo.IsDataBound)
{
MessageBox.Show("TRUE");
}
else
{
MessageBox.Show("FALSE");
}
似乎combo.datasource = list1拥有我所有的价值(&#34; C-C&#34;和&#34; C-O&#34;)。但是当我启动调试器或启动应用程序时,出现的消息框是combo.isdatabound = false的第二个消息框。通常,它应该是真的吗?