1. 程式人生 > >DataGridView手動填充資料(向單元格寫資料)

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);
                        }