无法将类型为“ System.Windows.Controls.DataGridTextColumn”的对象转换为类型为“ System.IConvertible”的错误

时间:2019-09-04 13:34:26

标签: c# wpf datagrid

我正在构建一个带有数据网格的WPF表单,当选中一个复选框以“选择”该行时,该数据网格将从数据网格中提取数据。

DOC_Number是一个int值,但它是从SQL Server提取的数据网格中的数据。我试图将其转换为字符串或文本,因为它告诉我它不能作为整数接受,现在它不会转换为字符串。

我不确定是否没有正确获取复选框数据,或者DOC_Number是否未注册为整数。

我遇到错误:

  

System.InvalidCastException:'无法将类型为'System.Windows.Controls.DataGridTextColumn'的对象转换为类型为'System.IConvertible'

int DocIdx = Convert.ToInt32(DOC_Number);

private void BtnGridUpdate_Click(object sender, EventArgs e)
{
    int employeeID = Convert.ToInt32(EmpID.Text);
    UpdateInternal0(employeeID);
} 

void UpdateInternal0(int employeeId)
{
    try
    {
        using (SqlConnection con = new SqlConnection(Properties.Settings.Default.adminConnectionString))
        {
            con.Open();

            foreach (DataRowView drv in InternalGrid0.SelectedItems)
            {
                DataRow row = drv.Row;
                bool isSelected = Convert.ToBoolean(drv.Row[0]);
                if (isSelected)
                {
                    string sqlString = "INSERT INTO ChecklistTransitionTable (RelatedEmployeeID, RelatedDocIdx) VALUES (@EmpID, @DOC_Number)";

                    using (SqlCommand cmd = new SqlCommand(sqlString, con))
                    {
                        for (int i = 0; i < InternalGrid0.Columns.Count; i++) 
                        {

                            int DocIdx = Convert.ToInt32(DOC_Number);
                            int employeeID = Convert.ToInt32(EmpID.Text);

                            cmd.Parameters.AddWithValue("@EmpID", employeeID = (int)row[i]);
                            cmd.Parameters.AddWithValue("@DOC_Number", DocIdx = (int)row[i]);

                            int result = cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();

                            Console.WriteLine("Test");
                            //Check Error
                            if (result < 0)
                            {
                                System.Windows.Forms.MessageBox.Show("Error inserting data into database!");
                            }
                        }
                    }
                }
            }
        }

        System.Windows.Forms.MessageBox.Show("Employee data successfully updated.");
    }

    catch (SqlException ex)
    {
        System.Windows.Forms.MessageBox.Show(string.Format("\nMessage ---\n{0}", ex.Message));
    }
}

1 个答案:

答案 0 :(得分:0)

for在列上循环是没有意义的。您应该能够从DataRow row对象获取文档ID:

foreach (DataRowView drv in InternalGrid0.SelectedItems)
{
    DataRow row = drv.Row;
    bool isSelected = Convert.ToBoolean(drv.Row[0]);
    if (isSelected)
    {
        string sqlString = "INSERT INTO ChecklistTransitionTable (RelatedEmployeeID, RelatedDocIdx) VALUES (@EmpID, @DOC_Number)";

        using (SqlCommand cmd = new SqlCommand(sqlString, con))
        {
            int DocIdx = Convert.ToInt32(row["DOC_Number"]);

            cmd.Parameters.AddWithValue("@EmpID", employeeId);
            cmd.Parameters.AddWithValue("@DOC_Number", DocIdx);

            int result = cmd.ExecuteNonQuery();
            if (result < 0)
            {
                System.Windows.Forms.MessageBox.Show("Error inserting data into database!");
            }
        }
    }
}