GridViewComboBoxColumn not keeping display value after leaving cell
我当前有一个组合框,显示未选中该单元格时的valuemember。您单击该单元格,然后显示成员将进行选择。进行选择并单击后,值成员确实更改为正确的数字,但是我想让它在离开单元格后显示显示成员。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | //class to hold the data private class PriorityData { public string PriorityName { get; set; } public string PriorityCode { get; set; } public Color PriorityColor { get; set; } } //Construct the Priority column _priorityColumn = new DataGridViewComboBoxColumn { ValueMember ="PriorityCode", DisplayMember ="PriorityName", Name ="Priority", DataPropertyName ="Priority", HeaderText ="Priority", SortMode = DataGridViewColumnSortMode.NotSortable }; private void LoadPriorityList() { try { using (var sqlConn = new SqlConnection(Program.sqlMgr.ConnectionString)) { const string query ="SELECT * FROM mcPriority"; using (var da = new SqlDataAdapter(query, sqlConn)) { var priorityTable = new DataTable(); priorityTable.Clear(); da.Fill(priorityTable); //Clear the list _priorityList.Clear(); //Add the list of shuttels from the database to the shuttle list for the shuttle combo box column foreach (DataRow row in priorityTable.Rows) { _priorityList.Add(new PriorityData() { PriorityName = row["PriorityName"].ToString(), PriorityCode = row["PriorityCode"].ToString(), PriorityColor = Color.FromArgb(row["ColorCode"].ToInt()) }); } //Bind the part combo box column to the shuttle list _priorityColumn.DataSource = _priorityList; } } } catch (Exception ex) { MessageBox.Show($"Message: {ex.Message}{Environment.NewLine + Environment.NewLine} Class: {nameof(PaintQueueData)} Method: {nameof(LoadPriorityList)}"); } } |
我已经能够很好地将数据输入到gridview中,所有这些都已验证,直到屏幕显示为止,displaymember的值的确是DisplayName。
仅从下拉菜单中选择一个选项,即显示显示成员。
然后单击离开单元格,它将还原为值成员。
任何帮助都将不胜感激,将其停留一会儿。
问题是我的PriorityCode的数据库数据类型为int,并且在代码中将其声明为字符串,导致我正在考虑的数据验证错误,因此默认为gridview值。无论如何,将我在sql中的数据类型与c#匹配都能达到目的。