在dataGrid的CurrentCellChanged事件中写事件不知下例对你有无用处:private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{
int x=this.dataGrid1.CurrentCell.RowNumber; //取行号
//int y=this.dataGrid1.CurrentCell.ColumnNumber; //取列号
MessageBox.Show(this.dataGrid1[x,0].ToString());//显示X行0列的值
}
{
int x=this.dataGrid1.CurrentCell.RowNumber; //取行号
//int y=this.dataGrid1.CurrentCell.ColumnNumber; //取列号
MessageBox.Show(this.dataGrid1[x,0].ToString());//显示X行0列的值
}
解决方案 »
- 如何通过openremotebasekey获取远程计算机的注册表内容
- 求助高人帮我看看我的C#代码线程调用控件的问题
- [IE编程]郁闷的pasteHTML
- C#清理正在使用的所有资源??
- Yahoo中多处见到的选项卡式信息面板如何制作?
- 求C# IO读写的问题!
- 如何使DropDownlist 绑定到XML文件.
- 徕卡DNA03水准仪数据处理软件开发求助
- 无法将类型为“System.DBNull”的对象强制转换为类型“System.Byte[]
- dropdownlist控件的值如何BINDING
- 动态的给TreeView,增加了几个结点treenode,如何给treenode写一个点击事件?
- 关于windows窗体的问题(up有分)
string connString = "server=localhost;database=hos;uid=sa;pwd=";
string sqlString = "SELECT * FROM ME_EXCEPREG"; DataSet _dataSet = null; try
{
// Connection object
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connString); // Create data adapter object
System.Data.SqlClient.SqlDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlString, connection);
// Create a dataset object and fill with data using data adapter's Fill method
_dataSet = new DataSet();
dataAdapter.Fill(_dataSet, "ME_EXCEPREG");
connection.Close();
}
catch(Exception ex)
{
MessageBox.Show("Problem with DB access-\n\n connection: "
+ connString + "\r\n\r\n query: " + sqlString
+ "\r\n\r\n\r\n" + ex.ToString());
this.Close();
return;
} // Create a table style that will hold the new column style
// that we set and also tie it to our customer's table from our DB
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "ME_EXCEPREG"; // make the dataGrid use our new tablestyle and bind it to our table
dataGrid1.TableStyles.Clear(); dataGrid1.TableStyles.Add(tableStyle);
dataGrid1.DataSource = _dataSet.Tables["ME_EXCEPREG"];
下面是
public void AutoSizeTable()
{
int numCols = ((DataTable) dataGrid1.DataSource).Columns.Count;
for(int i = 0; i < numCols; ++i)
AutoSizeCol(i);
}
public void AutoSizeCols(int start, int finish)
{
for(int i = start; i <= finish; ++i)
AutoSizeCol(i);
}
public void AutoSizeCol(int col)
{
float width = 0;
int numRows = ((DataTable) dataGrid1.DataSource).Rows.Count;
Graphics g = Graphics.FromHwnd(dataGrid1.Handle);
StringFormat sf = new StringFormat(StringFormat.GenericTypographic);
SizeF size; for(int i = 0; i < numRows; ++ i)
{
size = g.MeasureString(dataGrid1[i, col].ToString(), dataGrid1.Font, 500, sf);
if(size.Width > width)
width = size.Width;
} g.Dispose(); dataGrid1.TableStyles["ME_EXCEPREG"].GridColumnStyles[col].Width = (int) width;
}