TBD
21:33:12
在不呢,
|
TBD
21:33:40
请问Datagridvie控件问题
|
TBD
21:33:54
请问Datagridview控件问题
|
TBD
21:35:52
我在datagridview中通过datasource自动给它加入列,然后再加插入,编辑和删除,但是它的顺序老变,不定,那么我很难判定用户按了插入还还是其它的。因为e.columnIndex不定的。
|
TBD
21:36:15
我在datagridview中通过datasource自动给它加入列,然后再加插入,编辑和删除,但是它的顺序老变,不定,那么我很难判定用户按了插入还还是其它的。因为e.columnIndex不定的?怎么解决?
|
Michael
21:37:43
不明白
|
Michael
21:38:14
你的autogenerate是=true?
|
TBD
21:38:45
dataGrid.Columns.Clear(); DataTable table = dbManager.ReadDataFromDB(selectString); dataGrid.DataSource = table; dataGrid.AllowUserToAddRows = true; dataGrid.AllowUserToOrderColumns = true; dataGrid.Columns["RecordID"].Visible = false;
// insert edit button into datagridview insertButton = new DataGridViewButtonColumn(); insertButton.HeaderText = "插入"; insertButton.Text = "插入"; insertButton.Name = "edit"; insertButton.UseColumnTextForButtonValue = true; insertButton.Width = 40; dataGrid.Columns.Add(insertButton);
// insert edit button into datagridview editButton = new DataGridViewButtonColumn(); editButton.HeaderText = "编辑"; editButton.Text = "编辑"; editButton.Name = "edit"; editButton.UseColumnTextForButtonValue = true; editButton.Width = 40; dataGrid.Columns.Add(editButton);
// insert delete button to datagridview deleteButton = new DataGridViewButtonColumn(); deleteButton.HeaderText = "删除"; deleteButton.Text = "删除"; deleteButton.Name = "delete"; deleteButton.UseColumnTextForButtonValue = true; deleteButton.Width = 40; dataGrid.Columns.Add(deleteButton); 这样,第0列,第一列。。。。。顺序不定
|
TBD
21:39:09
对,是true, 如果是false,那就加不进呀。
|
Michael
21:40:58
是不是这个的影响dataGrid.AllowUserToOrderColumns = true;
|
TBD
21:41:44
这个我试过,没用的
|
Michael
21:42:50
要不autogenerate=false
|
Michael
21:42:59
自己定义那些栏
|
TBD
21:43:45
autogenerate=false,那样从数据库中的加入的列就加不进。
|
Michael
21:44:10
得自己定义各栏
|
Michael
21:44:26
我以前都是自己定义
|
Michael
21:44:44
麻烦点,但是自主些
|
TBD
21:45:51
只得用这种最笨的办法, 不能混合使用。
|
TBD
21:46:09
也许这就是微软的一个bug
|
Michael
21:47:55
不确定
|
Michael
21:48:07
要不你再多帖点代码
|
TBD
21:50:37
public partial class BTSetting : Form { DataBaseManager dbManager = null;
private const string selectBtSitting = "select RecordID, DeviceType AS 型名,DbaNo AS DBANo,Temp as 温度,AHigh as 等级A上限, ALow as 等级A下限 , BHigh as 等级B上限, BLow as 等级B下限 ,CHigh as 等级C上限, CLow as 等级C下限, DHigh as 等级D上限, DLow as 等级D下限,Range as 量程,TemperatureWatch as 温度监控,DmmNPLC as 万用表积分时间,DmmFRes4Wire as 万用表2线4线 from BtSetting order by RecordID ASC"; private const string selectUsers = "select RecordID, UserName as 用户名,Password as 密码 from Users"; DataGridViewButtonColumn editButton; DataGridViewButtonColumn deleteButton; DataGridViewButtonColumn insertButton;
public BTSetting() { InitializeComponent();
InitDataBaseManager();
InitLoadDataGridView(dataGridViewBtSetting, selectBtSitting);
InitLoadDataGridView(dataGridViewUserManager, selectUsers);
}
private void InitDataBaseManager() { string currentdir = Directory.GetCurrentDirectory(); DirectoryInfo dirIfo = Directory.GetParent(currentdir); string dbDir = Path.Combine(dirIfo.FullName, "DataBase"); dbManager = new DataBaseManager(); bool retVal = dbManager.SetDataBaseConnectString(dbDir, "ResistanceTest.mdb");
if (!retVal) { throw new Exception("数据库找不到!"); } }
private void InitLoadDataGridView(DataGridView dataGrid, string selectString) { dataGrid.Columns.Clear(); DataTable table = dbManager.ReadDataFromDB(selectString); dataGrid.DataSource = table; dataGrid.AllowUserToAddRows = true; dataGrid.AllowUserToOrderColumns = true; dataGrid.Columns["RecordID"].Visible = false; //dataGrid.AllowUserToOrderColumns = true; //dataGrid.AutoGenerateColumns = false;
// insert edit button into datagridview insertButton = new DataGridViewButtonColumn(); insertButton.HeaderText = "插入"; insertButton.Text = "插入"; insertButton.Name = "edit"; insertButton.UseColumnTextForButtonValue = true; insertButton.Width = 40; dataGrid.Columns.Add(insertButton);
// insert edit button into datagridview editButton = new DataGridViewButtonColumn(); editButton.HeaderText = "编辑"; editButton.Text = "编辑"; editButton.Name = "edit"; editButton.UseColumnTextForButtonValue = true; editButton.Width = 40; dataGrid.Columns.Add(editButton);
// insert delete button to datagridview deleteButton = new DataGridViewButtonColumn(); deleteButton.HeaderText = "删除"; deleteButton.Text = "删除"; deleteButton.Name = "delete"; deleteButton.UseColumnTextForButtonValue = true; deleteButton.Width = 40; dataGrid.Columns.Add(deleteButton); }
|
TBD
21:51:04
private void dataGridViewBtSetting_CellContentClick(object sender, DataGridViewCellEventArgs e) {
if (dataGridViewBtSetting.Columns[e.ColumnIndex] == insertButton && e.RowIndex >= 0) { string selectStr = "select max(RecordID) as theMaxRecordID from BtSetting"; DataTable table = dbManager.ReadDataFromDB(selectStr); int maxRecordID = Convert.ToInt32(table.Rows[0]["theMaxRecordID"]); int recordID = maxRecordID + 1; string deviceType = dataGridViewBtSetting["型名", e.RowIndex].Value.ToString(); string dbaNo = dataGridViewBtSetting["DBANo", e.RowIndex].Value.ToString(); string temp = dataGridViewBtSetting["温度", e.RowIndex].Value.ToString(); double aHigh = Convert.ToDouble(dataGridViewBtSetting["等级A上限", e.RowIndex].Value.ToString()); double aLow = Convert.ToDouble(dataGridViewBtSetting["等级A下限", e.RowIndex].Value.ToString()); double bHigh = Convert.ToDouble(dataGridViewBtSetting["等级B上限", e.RowIndex].Value.ToString()); double bLow = Convert.ToDouble(dataGridViewBtSetting["等级B下限", e.RowIndex].Value.ToString()); double cHigh = Convert.ToDouble(dataGridViewBtSetting["等级C上限", e.RowIndex].Value.ToString()); double cLow = Convert.ToDouble(dataGridViewBtSetting["等级C下限", e.RowIndex].Value.ToString()); double dHigh = Convert.ToDouble(dataGridViewBtSetting["等级D上限", e.RowIndex].Value.ToString()); double dLow = Convert.ToDouble(dataGridViewBtSetting["等级D下限", e.RowIndex].Value.ToString()); double range = Convert.ToDouble(dataGridViewBtSetting["量程", e.RowIndex].Value.ToString()); bool tempWatch = false; string tempStr = dataGridViewBtSetting["温度监控", e.RowIndex].Value.ToString(); if (tempStr != "") { tempWatch = Convert.ToBoolean(tempStr); }
string nplc = dataGridViewBtSetting["万用表积分时间", e.RowIndex].Value.ToString();
bool fourWare = false; tempStr = dataGridViewBtSetting["万用表2线4线", e.RowIndex].Value.ToString(); if (!string.IsNullOrEmpty(tempStr)) { fourWare = Convert.ToBoolean(tempStr); } string insertString = string.Format("insert into BtSetting values ({0},'{1}','{2}',{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15})", recordID, deviceType, dbaNo,temp, aHigh, aLow, bHigh, bLow, cHigh, cLow, dHigh, dLow, range, tempWatch, nplc, fourWare); bool isInsertOk = dbManager.UpdateDataToDB(insertString); if (!isInsertOk) { MessageBox.Show("数据插入没有成功,请重新插入或联系技术员。");
} InitLoadDataGridView(dataGridViewBtSetting, selectBtSitting); }
|
TBD
21:51:12
else if (dataGridViewBtSetting.Columns[e.ColumnIndex] == editButton && e.RowIndex >= 0) {
// The old record is edited,then updata int recordID = Convert.ToInt32(dataGridViewBtSetting["RecordID", e.RowIndex].Value.ToString()); string deviceType = dataGridViewBtSetting["型名", e.RowIndex].Value.ToString(); string dbaNo = dataGridViewBtSetting["DBANo", e.RowIndex].Value.ToString(); string temp = dataGridViewBtSetting["温度", e.RowIndex].Value.ToString(); double aHigh = Convert.ToDouble(dataGridViewBtSetting["等级A上限", e.RowIndex].Value.ToString()); double aLow = Convert.ToDouble(dataGridViewBtSetting["等级A下限", e.RowIndex].Value.ToString()); double bHigh = Convert.ToDouble(dataGridViewBtSetting["等级B上限", e.RowIndex].Value.ToString()); double bLow = Convert.ToDouble(dataGridViewBtSetting["等级B下限", e.RowIndex].Value.ToString()); double cHigh = Convert.ToDouble(dataGridViewBtSetting["等级C上限", e.RowIndex].Value.ToString()); double cLow = Convert.ToDouble(dataGridViewBtSetting["等级C下限", e.RowIndex].Value.ToString()); double dHigh = Convert.ToDouble(dataGridViewBtSetting["等级D上限", e.RowIndex].Value.ToString()); double dLow = Convert.ToDouble(dataGridViewBtSetting["等级D下限", e.RowIndex].Value.ToString()); double range = Convert.ToDouble(dataGridViewBtSetting["量程", e.RowIndex].Value.ToString()); bool tempWatch = Convert.ToBoolean(dataGridViewBtSetting["温度监控", e.RowIndex].Value.ToString()); string nplc = dataGridViewBtSetting["万用表积分时间", e.RowIndex].Value.ToString(); bool fourWare = Convert.ToBoolean(dataGridViewBtSetting["万用表2线4线", e.RowIndex].Value.ToString());
string updateString = string.Format("update BtSetting set RecordID={0},DeviceType='{1}',DBANo='{2}',Temp = {3},AHigh={4},ALow={5},BHigh={6},BLow={7},CHigh={8},CLow={9},DHigh={10},DLow={11},Range={12},TemperatureWatch={13},DmmNPLC={14},DmmFRes4Wire={15}", recordID, deviceType, dbaNo,temp, aHigh, aLow, bHigh, bLow, cHigh, cLow, dHigh, dLow, range, tempWatch, nplc, fourWare); updateString += " where RecordID =" + recordID.ToString(); bool isInsertOk = dbManager.UpdateDataToDB(updateString); if (!isInsertOk) { MessageBox.Show("数据更新没有成功,请重新更新或联系技术员。");
} InitLoadDataGridView(dataGridViewBtSetting, selectBtSitting); } // delete button else if (dataGridViewBtSetting.Columns[e.ColumnIndex] == deleteButton && e.RowIndex >= 0) { DialogResult result = MessageBox.Show("删除数据不可恢复,确定要永久删除吗?", "警告!", MessageBoxButtons.OKCancel); if (result == System.Windows.Forms.DialogResult.OK) { string deleteString = "delete from BtSetting where RecordID = " + dataGridViewBtSetting["RecordID", e.RowIndex].Value.ToString(); bool isUpdateOk = dbManager.UpdateDataToDB(deleteString); if (!isUpdateOk) { MessageBox.Show("记录没有删除掉,请再删除或者联系技术员!"); } } InitLoadDataGridView(dataGridViewBtSetting, selectBtSitting); } }
|
Michael
21:56:59
没有看到你的dataGrid.DataBind(). 在这句之后家上吧dataGrid.Columns.Add(deleteButton);
|
TBD
21:57:58
这个不用也可以吧,
|
TBD
21:58:12
DataBind作用是什么
|
Michael
21:58:42
就是绑定,
|
Michael
21:59:04
只是自己调用,时机掌握更好点
|
Michael
21:59:29
你最开始说的e.columnIndex老变化是吗?
|
TBD
21:59:35
对。
|
TBD
21:59:50
没有DataBind()这个函数,在2010里。
|
Michael
22:00:04
是在dataGridViewBtSetting_CellContentClick这个方法里面吗?
|
TBD
22:00:15
对。
|
Michael
22:01:57
这个是随便点那些单元格,
|
Michael
22:02:09
你点到哪里,就是哪里
|
Michael
22:03:28
e.RowIndex 和e.columnIndex肯定是变化的
|
Michael
22:03:56
应该将插入,编辑和删除的事件处理放到别的地方
|
Michael
22:04:27
比如你可以给这些插入,编辑和删除按钮都加上一个Command参数
|
TBD
22:07:48
不是的,本来删除是最后一例,e.columnIndex=15才对,可是它等于3.
|
TBD
22:08:32
再点下删除,它有可能等于15
|
Michael
22:08:42
在CellContentClick处理这个不好
|
TBD
22:10:17
deleteButton又没独立的事件呀?
|
Michael
22:11:09
可以给每个deleteButton加上时间处理的
|
Michael
22:11:33
事件处理
|
TBD
22:11:47
不能加呀,没有。
|
Michael
22:12:05
在gridview_ItemDatabound里面加
|
Michael
22:12:25
另外一种办法就是给它加Command参数
|
TBD
22:13:31
Command参数加在那?
|
Michael
22:14:46
InitLoadDataGridView方法里面
|
Michael
22:15:26
写上insertButton.Command = "insert"
|
TBD
22:15:35
gridview_ItemDatabound是datagridview的事件吗?
|
Michael
22:15:36
你看看可以写吗
|
TBD
22:15:44
哦。
|
TBD
22:16:54
NO,2010没有COMMAND这个属性了。
|
Michael
22:17:41
你的是这个类型DataGridViewButtonColumn
|
Michael
22:17:48
可能不一样
|
Michael
22:18:00
你等等,我试试
|
TBD
22:18:01
是呀。
|
Michael
22:22:46
你的是asp.net 4.0?
|
TBD
22:23:20
不是,winform
|
TBD
22:23:31
不是,winform4.0
|
Michael
22:23:41
呵呵,
|
Michael
22:24:02
理解错误,等等我试试Winform 4.0
|
TBD
22:24:08
因为最近接个私活,搞这个。呵呵。
|
Michael
22:24:34
哦,你在哪里接的?
|
TBD
22:25:35
深圳,一个朋友工厂做一个测试系统。
|
Michael
22:26:23
是,代码看起来是要卖钱的
|
TBD
22:26:52
可能不多,那人小气。
|
TBD
22:27:17
可能只够买个本本,还有可能买不到。晕死了。真不值钱。
|
TBD
22:48:37
怎么样。。。
|
Michael
22:49:12
还没有找到办法
|
TBD
22:49:26
|
Michael
22:49:31
我的机器慢,运行vs2010吃力
|
Michael
22:49:45
刚刚写好程序
|
Michael
23:10:03
发现一点
|
Michael
23:10:07
public Form1() { InitializeComponent(); Bind(); }
private void Bind() { dataGridView1.Columns.Clear(); DataTable table = new DataTable(); DataColumn col1 = new DataColumn(); col1.ColumnName = "col1"; col1.DataType = typeof(int); DataColumn col2 = new DataColumn(); col2.ColumnName = "col2"; col2.DataType = typeof(string); table.Columns.Add(col1); table.Columns.Add(col2); table.Rows.Add(1, "slsfldks"); table.Rows.Add(2, "fdsgfd5b4"); table.Rows.Add(3, "534st34"); table.Rows.Add(4, "fdsf43b35n3");
dataGridView1.DataSource = table; dataGridView1.AllowUserToAddRows = true; dataGridView1.AllowUserToOrderColumns = true; //dataGridView1.Columns["RecordID"].Visible = false; //dataGrid.AllowUserToOrderColumns = true; //dataGrid.AutoGenerateColumns = false;
// insert edit button into datagridview DataGridViewButtonColumn insertButton = new DataGridViewButtonColumn(); insertButton.HeaderText = "插入"; insertButton.Text = "插入"; insertButton.Name = "insert"; insertButton.UseColumnTextForButtonValue = true; insertButton.Width = 40;
dataGridView1.Columns.Add(insertButton);
// insert edit button into datagridview DataGridViewButtonColumn editButton = new DataGridViewButtonColumn(); editButton.HeaderText = "编辑"; editButton.Text = "编辑"; editButton.Name = "edit"; editButton.UseColumnTextForButtonValue = true; editButton.Width = 40; dataGridView1.Columns.Add(editButton);
// insert delete button to datagridview DataGridViewButtonColumn deleteButton = new DataGridViewButtonColumn(); deleteButton.HeaderText = "删除"; deleteButton.Text = "删除"; deleteButton.Name = "delete"; deleteButton.UseColumnTextForButtonValue = true; deleteButton.Width = 40; dataGridView1.Columns.Add(deleteButton);
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { MessageBox.Show(e.RowIndex.ToString() + dataGridView1.Columns[e.ColumnIndex].Name); }
|
Michael
23:10:30
用CellClick
|
TBD
23:11:08
e.ColumnIndex不是按你想要的顺序。
|
TBD
23:11:16
它乱给你一个index
|
Michael
23:11:25
用CellClick
|
Michael
23:11:38
不是用CellContentClick
|
TBD
23:11:49
那一样的吧。
|
Michael
23:12:50
呵呵,我没有觉得那里乱
|
Michael
23:13:01
这个index是对的啊
|
Michael
23:13:34
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); Bind(); }
private void Bind() { dataGridView1.Columns.Clear(); DataTable table = new DataTable(); DataColumn col1 = new DataColumn(); col1.ColumnName = "col1"; col1.DataType = typeof(int); DataColumn col2 = new DataColumn(); col2.ColumnName = "col2"; col2.DataType = typeof(string); table.Columns.Add(col1); table.Columns.Add(col2); table.Rows.Add(1, "slsfldks"); table.Rows.Add(2, "fdsgfd5b4"); table.Rows.Add(3, "534st34"); table.Rows.Add(4, "fdsf43b35n3");
dataGridView1.DataSource = table; dataGridView1.AllowUserToAddRows = true; dataGridView1.AllowUserToOrderColumns = true; //dataGridView1.Columns["RecordID"].Visible = false; //dataGrid.AllowUserToOrderColumns = true; //dataGrid.AutoGenerateColumns = false;
// insert edit button into datagridview DataGridViewButtonColumn insertButton = new DataGridViewButtonColumn(); insertButton.HeaderText = "插入"; insertButton.Text = "插入"; insertButton.Name = "insert"; insertButton.UseColumnTextForButtonValue = true; insertButton.Width = 40;
dataGridView1.Columns.Add(insertButton);
// insert edit button into datagridview DataGridViewButtonColumn editButton = new DataGridViewButtonColumn(); editButton.HeaderText = "编辑"; editButton.Text = "编辑"; editButton.Name = "edit"; editButton.UseColumnTextForButtonValue = true; editButton.Width = 40; dataGridView1.Columns.Add(editButton);
// insert delete button to datagridview DataGridViewButtonColumn deleteButton = new DataGridViewButtonColumn(); deleteButton.HeaderText = "删除"; deleteButton.Text = "删除"; deleteButton.Name = "delete"; deleteButton.UseColumnTextForButtonValue = true; deleteButton.Width = 40; dataGridView1.Columns.Add(deleteButton);
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { MessageBox.Show(e.RowIndex.ToString() + dataGridView1.Columns[e.ColumnIndex].Name); }
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { MessageBox.Show(e.RowIndex.ToString() + dataGridView1.Columns[e.ColumnIndex].Name); } } }
|
TBD
23:14:05
那就怪了。。。。。。
|
TBD
23:14:08
谢谢。。。。
|
Michael
23:14:38
你是要删除行?还是列?
|
TBD
23:14:57
行
|
Michael
23:16:32
我把整个工程都打包了
|
Michael
23:16:40
你可以看看
|
TBD
23:16:48
谢谢你呀。呵呵。
|
23:16:52
23:16:52 成功发送文件“WindowsFormsApplication1.rar”(40.00KB)。
|
Michael
23:16:56
我没有觉得乱,
|
TBD
23:17:07
哦,好。
|
TBD
23:17:15
我再仔细看看。
|
Michael
23:17:16
index都是正常的
|
TBD
23:17:32
多删除几次就乱了。。。
|
Michael
23:17:55
哦?
|
Michael
23:18:08
可能是需要重新绑定
|
TBD
23:21:18
试下。
|