环境:vs2005 ,SQL2005
数据表: sj(cusid,name,pid,audit,comment)
(int,varchar,int,varchar,varchar)
客户编号,客户名称,销售id,审批,意见
窗体:客户信息的添加审核 frmsh在主管审批销售添加客户的申请管理我将2个combobox添加到datagridview----dgv_tj中。希望实现的功能:
主管在审核销售提交的客户信息时,分别选择combobox中的审批和意见两个字段,然后点击更新按钮,实现批量更新数据。遇到的问题:
当sj表中的字段audit,comment不是空值时,操作没有出现错误;
当datagridview提取的字段audit,comment是空值时,将光标移到第四列combobox时,出现错误:unable to cast object of type‘System.DBNull’to type‘System.String’代码如下:public partial class frmsh : Form
{
private int comboBoxColumnIndex =4;
private int comboBoxColumnIndex1 = 5; private DataTable DT = new DataTable();
private SqlDataAdapter SDA = new SqlDataAdapter(); public frmsh()
{
InitializeComponent();
InitComboBoxValues();
this.dgv_tj.Controls.Add(this.comboBox1);
this.dgv_tj.Controls.Add(this.comboBox2);
this.dgv_tj.CellEnter += new DataGridViewCellEventHandler(dgv_tj_CellEnter);
this.dgv_tj.CellLeave += new DataGridViewCellEventHandler(dgv_tj_CellLeave); }
private void InitComboBoxValues()
{
this.comboBox1.Items.AddRange(new String[] { "通过", "驳回" });
this.comboBox2.Items.AddRange(new String[] { "符合要求", "保护对象,不允许跟踪", "特批,允许跟踪"});
this.comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
this.comboBox2.AutoCompleteMode = AutoCompleteMode.Suggest;
this.comboBox2.AutoCompleteSource = AutoCompleteSource.ListItems;
}
private void dgv_tj_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == comboBoxColumnIndex)
{
DataGridViewCell cell = this.dgv_tj.Rows[e.RowIndex].Cells[e.ColumnIndex];
Rectangle rect = this.dgv_tj.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true);
this.comboBox1.Location = rect.Location;
this.comboBox1.Size = rect.Size;
comfirmComboBoxValue(this.comboBox1, (String)cell.Value);
this.comboBox1.Visible = true;
}
else if (e.ColumnIndex ==comboBoxColumnIndex1)
{
DataGridViewCell cell = this.dgv_tj.Rows[e.RowIndex].Cells[e.ColumnIndex];
Rectangle rect = this.dgv_tj.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true);
this.comboBox2.Location = rect.Location;
this.comboBox2.Size = rect.Size;
comfirmComboBoxValue(this.comboBox2, (string)cell.Value);
this.comboBox2.Visible = true;
}
} private void dgv_tj_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == comboBoxColumnIndex)
{
DataGridViewCell cell = this.dgv_tj.Rows[e.RowIndex].Cells[e.ColumnIndex];
cell.Value = this.comboBox1.Text;
this.comboBox1.Visible = false;
}
else
if (e.ColumnIndex == comboBoxColumnIndex1)
{
DataGridViewCell cell = this.dgv_tj.Rows[e.RowIndex].Cells[e.ColumnIndex];
cell.Value = this.comboBox2.Text;
this.comboBox2.Visible = false;
}
} private void comfirmComboBoxValue(ComboBox com, String cellValue)
{ com.SelectedIndex = -1;
if (cellValue == null) //问题出现在这里!!!???请问应该如何修改?
{
com.Text = " ";
return;
}
com.Text = cellValue;
foreach (Object item in com.Items)
{
if ((String)item == cellValue)
{
com.SelectedItem = item;
}
}
}
private void frmsh_Load(object sender, EventArgs e)
{
string sqlstr = "select * from sj where audit is null ";
string con = "server=T61;database=customer;uid=sa;pwd=";
SqlConnection conn = new SqlConnection(con);
SqlCommand SCD = new SqlCommand(sqlstr ,conn);
SDA.SelectCommand = SCD;
SDA.Fill(DT);
dgv_tj.DataSource = DT;
dgv_tj.AllowUserToAddRows = false;
}
//给datagridview加行号
private void dgv_tj_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
dgv_tj.RowHeadersWidth - 4,
e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
dgv_tj.RowHeadersDefaultCellStyle.Font,
rectangle,
dgv_tj.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
} private void btupdate_Click(object sender, EventArgs e)
{
try
{
DataTable result = dgv_tj.DataSource as DataTable;
DT = result;
SqlDataAdapter sda = new SqlDataAdapter();
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=T61;database=customer;uid=sa;pwd=";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "update sj set audit=@audit,comment=@comment where cusid=@cusid";
cmd.Parameters.Add("@audit", SqlDbType.VarChar, 4);
cmd.Parameters[0].SourceColumn = "audit";
cmd.Parameters.Add("@comment", SqlDbType.VarChar,100);
cmd.Parameters[1].SourceColumn = "comment";
cmd.Parameters.Add("@cusid", SqlDbType.Int);
cmd.Parameters[2].SourceColumn = "cusid";
sda.UpdateCommand = cmd;
sda.Update(DT);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
MessageBox.Show("更新成功!"); } }希望得到帮助谢谢!
数据表: sj(cusid,name,pid,audit,comment)
(int,varchar,int,varchar,varchar)
客户编号,客户名称,销售id,审批,意见
窗体:客户信息的添加审核 frmsh在主管审批销售添加客户的申请管理我将2个combobox添加到datagridview----dgv_tj中。希望实现的功能:
主管在审核销售提交的客户信息时,分别选择combobox中的审批和意见两个字段,然后点击更新按钮,实现批量更新数据。遇到的问题:
当sj表中的字段audit,comment不是空值时,操作没有出现错误;
当datagridview提取的字段audit,comment是空值时,将光标移到第四列combobox时,出现错误:unable to cast object of type‘System.DBNull’to type‘System.String’代码如下:public partial class frmsh : Form
{
private int comboBoxColumnIndex =4;
private int comboBoxColumnIndex1 = 5; private DataTable DT = new DataTable();
private SqlDataAdapter SDA = new SqlDataAdapter(); public frmsh()
{
InitializeComponent();
InitComboBoxValues();
this.dgv_tj.Controls.Add(this.comboBox1);
this.dgv_tj.Controls.Add(this.comboBox2);
this.dgv_tj.CellEnter += new DataGridViewCellEventHandler(dgv_tj_CellEnter);
this.dgv_tj.CellLeave += new DataGridViewCellEventHandler(dgv_tj_CellLeave); }
private void InitComboBoxValues()
{
this.comboBox1.Items.AddRange(new String[] { "通过", "驳回" });
this.comboBox2.Items.AddRange(new String[] { "符合要求", "保护对象,不允许跟踪", "特批,允许跟踪"});
this.comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
this.comboBox2.AutoCompleteMode = AutoCompleteMode.Suggest;
this.comboBox2.AutoCompleteSource = AutoCompleteSource.ListItems;
}
private void dgv_tj_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == comboBoxColumnIndex)
{
DataGridViewCell cell = this.dgv_tj.Rows[e.RowIndex].Cells[e.ColumnIndex];
Rectangle rect = this.dgv_tj.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true);
this.comboBox1.Location = rect.Location;
this.comboBox1.Size = rect.Size;
comfirmComboBoxValue(this.comboBox1, (String)cell.Value);
this.comboBox1.Visible = true;
}
else if (e.ColumnIndex ==comboBoxColumnIndex1)
{
DataGridViewCell cell = this.dgv_tj.Rows[e.RowIndex].Cells[e.ColumnIndex];
Rectangle rect = this.dgv_tj.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true);
this.comboBox2.Location = rect.Location;
this.comboBox2.Size = rect.Size;
comfirmComboBoxValue(this.comboBox2, (string)cell.Value);
this.comboBox2.Visible = true;
}
} private void dgv_tj_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == comboBoxColumnIndex)
{
DataGridViewCell cell = this.dgv_tj.Rows[e.RowIndex].Cells[e.ColumnIndex];
cell.Value = this.comboBox1.Text;
this.comboBox1.Visible = false;
}
else
if (e.ColumnIndex == comboBoxColumnIndex1)
{
DataGridViewCell cell = this.dgv_tj.Rows[e.RowIndex].Cells[e.ColumnIndex];
cell.Value = this.comboBox2.Text;
this.comboBox2.Visible = false;
}
} private void comfirmComboBoxValue(ComboBox com, String cellValue)
{ com.SelectedIndex = -1;
if (cellValue == null) //问题出现在这里!!!???请问应该如何修改?
{
com.Text = " ";
return;
}
com.Text = cellValue;
foreach (Object item in com.Items)
{
if ((String)item == cellValue)
{
com.SelectedItem = item;
}
}
}
private void frmsh_Load(object sender, EventArgs e)
{
string sqlstr = "select * from sj where audit is null ";
string con = "server=T61;database=customer;uid=sa;pwd=";
SqlConnection conn = new SqlConnection(con);
SqlCommand SCD = new SqlCommand(sqlstr ,conn);
SDA.SelectCommand = SCD;
SDA.Fill(DT);
dgv_tj.DataSource = DT;
dgv_tj.AllowUserToAddRows = false;
}
//给datagridview加行号
private void dgv_tj_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
dgv_tj.RowHeadersWidth - 4,
e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
dgv_tj.RowHeadersDefaultCellStyle.Font,
rectangle,
dgv_tj.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
} private void btupdate_Click(object sender, EventArgs e)
{
try
{
DataTable result = dgv_tj.DataSource as DataTable;
DT = result;
SqlDataAdapter sda = new SqlDataAdapter();
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=T61;database=customer;uid=sa;pwd=";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "update sj set audit=@audit,comment=@comment where cusid=@cusid";
cmd.Parameters.Add("@audit", SqlDbType.VarChar, 4);
cmd.Parameters[0].SourceColumn = "audit";
cmd.Parameters.Add("@comment", SqlDbType.VarChar,100);
cmd.Parameters[1].SourceColumn = "comment";
cmd.Parameters.Add("@cusid", SqlDbType.Int);
cmd.Parameters[2].SourceColumn = "cusid";
sda.UpdateCommand = cmd;
sda.Update(DT);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
MessageBox.Show("更新成功!"); } }希望得到帮助谢谢!
解决方案 »
- C#关于串口发送数据listview控件接收
- javascript 实现文件下载
- 工作日功能
- 求一个数据操作时的优化
- 32bit操作系统开发的.net winform程序可以在64bit的win2003下运行吗?
- 在Microsoft Visual Studio .NET 2003中做c++为什么#include "iostream.h"就出错呀??
- 请求各位高手指点指点,踊跃发言,多给点意见!跪谢。。。。
- 一个简单问题,急!!,谢谢!!
- 对Socket的困惑!
- udp协议的问题
- 自定义控件属性的初始化问题
- object.MemberwiseClone()为什么可以复制string类型的值,string是引用类型啊
{
if (e.ColumnIndex == comboBoxColumnIndex)
{
DataGridViewCell cell = this.dgv_tj.Rows[e.RowIndex].Cells[e.ColumnIndex];
Rectangle rect = this.dgv_tj.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true);
this.comboBox1.Location = rect.Location;
this.comboBox1.Size = rect.Size;
comfirmComboBoxValue(this.comboBox1, cell.Value == DBNull.Value ? null :(String)cell.Value);
this.comboBox1.Visible = true;
}
else if (e.ColumnIndex ==comboBoxColumnIndex1)
{
DataGridViewCell cell = this.dgv_tj.Rows[e.RowIndex].Cells[e.ColumnIndex];
Rectangle rect = this.dgv_tj.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true);
this.comboBox2.Location = rect.Location;
this.comboBox2.Size = rect.Size;
comfirmComboBoxValue(this.comboBox2, cell.Value == DBNull.Value ? null :(String)cell.Value);
this.comboBox2.Visible = true;
}
}