asp.net gridview怎样实现window下的效果
我简单画个图
-------------------------------------------------
编码 | 名称 | 价格 |
@1 | @2 | @3 |
上面的是在GridView中,我在@1单元格处,点击时,就变成输入框,我就可以在里面输入编码,当我输完后,按回车键,后面的@2,@3就会根据@1处我输入文本框中的编码到数据库中把对应的字段显示出来,
------解决方案--------------------我写了一个,代码比较乱,但是大体上能实现你所说的。给你参考参考!
代码:
public partial class GridView2 : System.Web.UI.Page
{
private DataTable dt1;//显示数据用的
private SqlConnection conn;
private SqlCommand select;
private SqlDataAdapter sd1;
public GridView2()
{
this.conn = new SqlConnection(@ "Data Source=ibm003\sx813;Initial Catalog=Northwind;uid=fengdong;password=fengdong ");
this.select = new SqlCommand();
this.select.Connection = conn;
this.sd1 = new SqlDataAdapter(this.select);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
initDataTable();
insertEmptyRow();
bindData();
}
}
//初始化保存结果的DataTable
private void initDataTable()
{
//设置好DataTable的列
dt1 = new DataTable( "Table1 ");
dt1.Columns.Add( "EmployeeID ");
dt1.Columns.Add( "FirstName ");
dt1.Columns.Add( "LastName ");
}
private void insertEmptyRow()
{
//添加一个空行,供用户填写
DataRow dr = dt1.NewRow();
dr[0] = "请填写代码 ";
dr[1] = " ";
dr[2] = " ";
dt1.Rows.Add(dr);
}
//将产寻到的结果添加进存放结果DataTable
private void insertRow(DataRow dr)
{
this.dt1.Rows.Add(dr.ItemArray);
this.setData();//
}
private void bindData()
{
this.GridView1.DataSource = this.dt1;
this.GridView1.DataBind();
}
//从ViewState中获取以有的数据
private void getData()
{
if (null != ViewState[ "data1 "])
{
this.dt1 = (DataTable)ViewState[ "data1 "];
}
}
//将结果保存至ViewState
private void setData()
{
int a = dt1.Rows.Count;
for (int i=0; i <a; i++)
{
DataRow dr = dt1.Rows[i];
if (dr[ "EmployeeID "].ToString() == "请填写代码 ")
{
dt1.Rows.Remove(dr);
a--;
}
}
ViewState.Add( "data1 ",dt1);
}
private void loadData()
{
initDataTable();
getData();
}
protected void tbx_1_TextChanged(object sender, EventArgs e)
{
TextBox t1 = (TextBox)sender;
string sql = "select EmployeeID,FirstName,LastName from Employees where EmployeeID= " + t1.Text;
this.select.CommandText = sql;
DataTable tab1 = new DataTable();
this.sd1.Fill(tab1);
loadData();
if (tab1.Rows.Count == 1)
{
DataRow dr1 = dt1.NewRow();
dr1 = tab1.Rows[0];
insertRow(dr1);
insertEmptyRow();
bindData();
}