DataGridView控件的使用方法,结果异常!请指教!
public partial class Form1 : Form{
public Form1()
{
InitializeComponent();
}
static string connectionString = "Data Source=.;Initial Catalog=temdp;Integrated Security=True";
SqlConnection conn = new SqlConnection(connectionString);
SqlDataAdapter Adapter;
DataSet dataSet = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
string selectString = "select * from tb_DataGridView_2";
Adapter = new SqlDataAdapter(selectString, conn);
Adapter.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables;
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if(e.ColumnIndex==2&&e.RowIndex!=-1||e.ColumnIndex==3&&e.RowIndex!=-1
||e.ColumnIndex==4&&e.RowIndex!=-1)
{
Brush Bsh_1 = new SolidBrush(dataGridView1.GridColor);
SolidBrush GLBSH = new SolidBrush(e.CellStyle.BackColor);
using (Pen datagridLinePen = new Pen(Bsh_1))
{
e.Graphics.FillRectangle(GLBSH, e.ClipBounds);
if (e.RowIndex < dataGridView1.Rows.Count - 1 && dataGridView1.Rows.
Cells.Value != null &&dataGridView1.Rows.Cells[e.
ColumnIndex].Value.ToString() !=e.Value.ToString())
{
e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right, e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
e.CellBounds.Right - 1, e.CellBounds.Bottom);
}
else
e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Right-1, e.CellBounds.Top,
e.CellBounds.Right-1, e.CellBounds.Bottom );
if (e.RowIndex == dataGridView1.Rows.Count - 1)
e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right, e.CellBounds.Bottom - 1);
if (e.Value != null)
if(!(e.RowIndex>0&&dataGridView1.Rows.Cells.Value.
ToString()==e.Value.ToString()))
e.Graphics.DrawString(e.Value.ToString(),e.CellStyle.Font,Brushes.Black,e.
CellBounds.X+2,e.CellBounds.Y+5,StringFormat.GenericDefault);
e.Handled = true;
}
}
}
}
这个是DataGridView控件的使用(书上学的),作用是合并DataGridView控件中的单元格,在SQL中创建一张表格并且显示在DataGridView控件上,有相关的代码相信各位大蛇呢能够看懂。。。之所以发上论坛!是因为虽然这个没有逻辑错误。。。但是在运行之后会出现看不见表格的情况,简单的说就是一种异常,有时间的可以帮我看一下。。。 在窗体加载事件中绑定,你的事件又错了 现在才看见这个回复:L这个是书上的,事件什么的我知道在哪里,但是绑定我就不知道你在说什么了。。。还有就是请问我抄袭书上的这个代码哪里错掉了? 谢谢分享!!! 谢谢分享!!!
页:
[1]