DataGridView手动填充数据(向单元格写数据)

问题描述:DataGridView的数据源是从数据库中查询到的一张表dataTable(userId,userName,Sex),其中sex为bit类型,出      来的是true和false,但要在单元格中显示为男女。

解决方法1:

手动遍历dataTable,添加到DataGridView中

                        string id = null;
                        string name = null;
                        string sex = null;
                        for (int i = 0; i < dataTable.Rows.Count; i++)
                        {
                            id = dataTable.Rows[i]["userId"].ToString();
                            name = dataTable.Rows[i]["userName"].ToString();
                            if ((bool)(dataTable.Rows[i]["sex"]) == true)
                                sex = "男";
                            else
                                sex = "女";
                            dgvSearchResult.Rows.Add(id,name,sex);
                        }

你可能感兴趣的:(C#)