{"id":236973,"date":"2022-03-16T10:35:42","date_gmt":"2022-03-16T02:35:42","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=236973"},"modified":"2022-03-03T10:37:21","modified_gmt":"2022-03-03T02:37:21","slug":"data-grid-view","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/data-grid-view.html","title":{"rendered":"DataGridView\u6e05\u9664\u663e\u793a\u7684\u6570\u636e\u3001\u8bbe\u5b9a\u53f3\u952e\u83dc\u5355\u8be6\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u4e86DataGridView\u6e05\u9664\u663e\u793a\u7684\u6570\u636e\u3001\u8bbe\u5b9a\u53f3\u952e\u83dc\u5355\u7684\u65b9\u6cd5\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u5b66\u4e60\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u4eec\u4e0b\u9762\u968f\u7740\u5c0f\u7f16\u6765\u4e00\u8d77\u5b66\u4e60\u5b66\u4e60\u5427<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e00\u3001\u6e05\u7a7a\u6570\u636e<\/strong><\/div>\n
1\u3001DataGridView\u672a\u7ed1\u5b9a\u6570\u636e\u65f6\u6e05\u7a7a\u6570\u636e<\/strong><\/span><\/div>\n
this.dgv_PropDemo.DataSource = null<\/pre>\n
2\u3001DataGridView\u7ed1\u5b9a\u6570\u636e\u65f6\u6e05\u7a7a\u6570\u636e<\/strong><\/span><\/div>\n

DataGridView\u7ed1\u5b9a\u4e86\u6570\u636e\u5c31\u4e0d\u80fd\u4f7f\u7528this.dgv_PropDemo.DataSource = null\u6e05\u7a7a\u6570\u636e\u4e86\uff0c\u4f7f\u7528this.dgv_PropDemo.DataSource = null\u4e0d\u4ec5\u4f1a\u6e05\u7a7a\u6570\u636e\uff0c\u800c\u4e14\u4e5f\u4f1a\u628aDataGridView\u7684\u5217\u6e05\u7a7a\u6389\uff0c\u8fd9\u65f6\u5c31\u8981\u4f7f\u7528\u5982\u4e0b\u7684\u4ee3\u7801\u6e05\u7a7a\u663e\u793a\u7684\u6570\u636e\uff1a<\/p>\n

DataTable dt = this.dgv_PropDemo.DataSource as DataTable;\r\ndt.Rows.Clear();\r\nthis.dgv_PropDemo.DataSource = dt;<\/pre>\n
\u4e8c\u3001\u8bbe\u5b9a\u53f3\u952e\u83dc\u5355 <\/strong><\/div>\n

DataGridView,DataGridViewColumn,DataGridViewRow,DataGridViewCell\u6709ContextMenuStrip\u5c5e\u6027\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eContextMenuStrip\u5bf9\u8c61\u6765\u63a7\u5236DataGridView\u7684\u53f3\u952e\u83dc\u5355\u7684\u663e\u793a\u3002<\/p>\n

DataGridViewColumn\u7684ContextMenuStrip\u5c5e\u6027\u8bbe\u5b9a\u9664\u4e86\u5217\u5934\u4ee5\u5916\u7684\u5355\u5143\u683c\u7684\u53f3\u952e\u83dc\u5355\u3002<\/p>\n

DataGridViewRow\u7684ContextMenuStrip\u5c5e\u6027\u8bbe\u5b9a\u9664\u4e86\u884c\u5934\u4ee5\u5916\u7684\u5355\u5143\u683c\u7684\u53f3\u952e\u83dc\u5355\u3002<\/p>\n

DataGridViewCell\u7684ContextMenuStrip\u5c5e\u6027\u8bbe\u5b9a\u6307\u5b9a\u5355\u5143\u683c\u7684\u53f3\u952e\u83dc\u5355\u3002<\/p>\n

\u5bf9\u4e8e\u5355\u5143\u683c\u4e0a\u7684\u53f3\u952e\u83dc\u5355\u7684\u8bbe\u5b9a\uff0c\u4f18\u5148\u987a\u5e8f\u662f:Cell>Row>Column>DataGridView<\/p>\n

\u5229\u7528CellContextMenuStripNeeded\u3001RowContextMenuStripNeeded\u4e8b\u4ef6\u53ef\u4ee5\u8bbe\u5b9a\u5355\u5143\u683c\u7684\u53f3\u952e\u83dc\u5355\uff0c\u5c24\u5176\u662f\u9700\u8981\u53f3\u952e\u83dc\u5355\u6839\u636e\u5355\u5143\u683c\u503c\u7684\u53d8\u5316\u800c\u53d8\u5316\u7684\u65f6\u5019\u3002\u6bd4\u8d77\u4f7f\u7528\u5faa\u73af\u904d\u5386\uff0c\u4f7f\u7528\u8be5\u4e8b\u4ef6\u6765\u8bbe\u5b9a\u53f3\u952e\u83dc\u5355\u7684\u6548\u7387\u66f4\u9ad8\u3002<\/p>\n

\u8bf4\u660e\uff1aCellContextMenuStripNeeded\u4e8b\u4ef6\u5904\u7406\u65b9\u6cd5\u7684\u53c2\u6570\u4e2d\uff0ce.RowIndex=-1\u8868\u793a\u5217\u5934\uff0ce.ColumnIndex=-1\u8868\u793a\u884c\u5934\u3002RowContextMenuStripNeeded\u5219\u4e0d\u5b58\u5728e.ColumnIndex=-1\u7684\u60c5\u51b5\u3002<\/p>\n

\u793a\u4f8b\u4e00\uff1a<\/strong><\/span><\/div>\n
\/\/\u8bbe\u7f6eDataGridView\u7684\u53f3\u952e\u83dc\u5355\r\nthis.dgv_Users.ContextMenuStrip = cmsDgv;\r\n\/\/\u8bbe\u7f6e\u5217\u7684\u53f3\u952e\u83dc\u5355\r\nthis.dgv_Users.Columns[1].ContextMenuStrip = cmsColumn;\r\n\/\/\u8bbe\u7f6e\u5217\u5934\u7684\u53f3\u952e\u83dc\u5355\r\nthis.dgv_Users.Columns[1].HeaderCell.ContextMenuStrip = cmsHeaderCell;\r\n\/\/\u8bbe\u7f6e\u884c\u7684\u53f3\u952e\u83dc\u5355\r\nthis.dgv_Users.Rows[2].ContextMenuStrip = cmsRow;\r\n\/\/\u8bbe\u7f6e\u5355\u5143\u683c\u7684\u53f3\u952e\u83dc\u5355\r\nthis.dgv_Users[1, 2].ContextMenuStrip = cmsCell;<\/pre>\n
\u793a\u4f8b\u4e8c\uff1a<\/strong><\/span><\/div>\n
private void dgv_Users_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)\r\n{\r\n    DataGridView dgv = sender as DataGridView;\r\n    if (e.RowIndex < 0)\r\n    {\r\n         \/\/\u8bbe\u7f6e\u5217\u5934\u53f3\u952e\r\n         e.ContextMenuStrip = cmsHeaderCell;\r\n    }\r\n    else if (e.ColumnIndex < 0)\r\n    { \r\n          \/\/\u8bbe\u7f6e\u884c\u5934\u53f3\u952e\u83dc\u5355\r\n          e.ContextMenuStrip = cmsRow;\r\n     }\r\n     else if (dgv[e.ColumnIndex, e.RowIndex].Value.ToString().Equals(\"\u7537\"))\r\n     {\r\n           e.ContextMenuStrip = cmsCell;\r\n     }\r\n     else\r\n     {\r\n           e.ContextMenuStrip = cmsDgv;\r\n     }\r\n}<\/pre>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8eDataGridView\u6e05\u9664\u663e\u793a\u7684\u6570\u636e\u3001\u8bbe\u5b9a\u53f3\u952e\u83dc\u5355\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

this.dgv_PropDemo.DataSource = null DataGridView\u7ed1\u5b9a\u4e86\u6570\u636e\u5c31\u4e0d […]<\/p>\n","protected":false},"author":1482,"featured_media":91491,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-236973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/236973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=236973"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/236973\/revisions"}],"predecessor-version":[{"id":236976,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/236973\/revisions\/236976"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/91491"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=236973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=236973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=236973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}