如何修复datagridview错误?

时间:2017-08-15 23:04:44

标签: c# winforms c#-4.0

我有以下代码:

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    string age = dataGridView1.Rows[e.RowIndex].Cells["dataGridViewTextBoxColumn43"].Value.ToString();

    dataGridView1.Rows[e.RowIndex].Cells["dataGridViewTextBoxColumn43"].Value = (age == "1") ? "Муж" : "Жен";
}

当我运行程序时,它会给出致命的消息:

enter image description here

在数据库字段gender中,integer类型为dataGridViewTextBoxColumn43。此值应添加到列名: return db.GetDataTable("SELECT " + "Pacients.id, " + "unique_code," + "status_pass, " + "payment, " + "profession," + "office_address, " + "factory_name, " + "factory_edrpou, " + "factory_departament," + "name, " + "secondname, " + "lastname, " + "datebirth, " + "taxcode, " + "gender, " + "Pacients.created_at, " + "file, " + "PacientsOrder.kind_work, " + "PacientsOrder.status " + "FROM Pacients LEFT JOIN PacientsOrder ON PacientsOrder.pacient_id = Pacients.id LEFT JOIN Transactions ON Transactions.pacient_id = Pacients.id ORDER BY Pacients.id DESC"); }

查询数据库:

public DataTable select(SQLiteDatabase db)     {

<table class="table table-hover">
            <thead>
              <tr>
                <th>No.</th>
                <th>Name</th>
                <th>Type</th>
                <th>Phone</th>
                <th>S.N.</th>
                <th>Date</th>
              </tr>
            </thead>
            <tbody>
              <tr style="cursor:pointer" onclick="mandaDetalle(this.id)">
                <td>PE-6</td>
                <td>John Smith</td>
                <td>GT5</td>
                <td>845254585</td>
                <td>56456456456</td>
                <td>14/07/2017</td>
              </tr>
            </tbody>
          </table>

3 个答案:

答案 0 :(得分:1)

如果列ValueTy‌​pe是数字,您可以将设计器中的数字格式更改为Муж;;Жен

dataGridView1.Columns["dataGridViewTextBoxColumn43"].DefaultCellStyle.Format = "Муж;;Жен";

答案 1 :(得分:0)

如果我理解正确,您正在尝试根据当前位于同一列中的整数值(即切换列值类型)向列添加文本值?

请不要这样做,因为它有几个原因不起作用。除了您的错误之外,最重要的是当您为创建的每一行设置DataGridView的DataSource时会触发此事件,但e.RowIndex并不总是新行的RowIndex。有关详细信息,请阅读here

通常,当用户从界面插入新行时,您只会使用RowsAdded。

您应该做的是在SELECT语句中提供从数据库中读取数据的转换。这样,您无需在数据访问DataGridView时转换数据。所以你的SELECT需要像:

SELECT .... CASE WHEN gender = 1 THEN 'МУЖ' ELSE 'ЖЕН' END AS gender ...

修改

尝试更改SELECT语句,使其如下所示:

public DataTable select(SQLiteDatabase db) {
    return db.GetDataTable("SELECT " +
        "Pacients.id, " +
        "unique_code," +
        "status_pass, " +
        "payment, " +
        "profession," +
        "office_address, " +
        "factory_name, " +
        "factory_edrpou, " +
        "factory_departament," +
        "name, " +
        "secondname, " +
        "lastname, " +
        "datebirth, " +
        "taxcode, " +
        "CASE WHEN gender = 1 THEN 'МУЖ' ELSE 'ЖЕН' END AS gender, " +
        "Pacients.created_at, " +
        "file,  " +
        "PacientsOrder.kind_work, " +
        "PacientsOrder.status " +
        "FROM Pacients LEFT JOIN PacientsOrder ON PacientsOrder.pacient_id = Pacients.id LEFT JOIN Transactions ON Transactions.pacient_id = Pacients.id ORDER BY Pacients.id DESC");
}

现在,您可以完全摆脱添加行事件,因为数据库已经为您完成了工作,然后才能访问datagridview。

答案 2 :(得分:0)

问题出在下面,它是obvios,但不是立即明确:

gender字段以integer类型存储在数据库中。

因此,当我将DataGridView与现有数据库表绑定时,它为构建表分配内存,并使用defination表的所有属性,包括单元格类型。

因此,当它第一次发生时,datagridview期望获取整数数据类型作为值,但我将值单元格修改为字符串值并尝试覆盖值。

这是一个致命的错误。

解决方案是将所有数据类型更改为integerstring类型。

感谢您的回答,我们会接受!