主题:[讨论]DataGrid如何才能更好的绑定数据呢?(看了就知道)
我是一名Asp.Net的初学者,最近,在学习DataGrid绑定数据的时候,总觉得哪里好像不对。但是,又查不出错误!比如下面在阶段考试时,我打得一段DataGrid绑定数据的代码.
请各位老鸟们帮我看一下:
default.aspx中:
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label ID="lblstuno" runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.stuNo") %> '> </asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtstuno" runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.stuNo") %> '> </asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label ID="stuname1" runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.stuName") %> '> </asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtstuname" runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.stuName") %> '> </asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label ID="stuaddr1" runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.stuAddress") %> '> </asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtstuaddr" runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.stuAddress") %> '> </asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="Button1" runat="server" CommandName="btnaddnew" Text="AddNew" />
</FooterTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn CommandName="Select" Text="选择"> </asp:ButtonColumn>
<asp:EditCommandColumn CancelText="取消" EditText="编辑" UpdateText="更新"> </asp:EditCommandColumn>
<asp:ButtonColumn CommandName="Delete" Text="删除"> </asp:ButtonColumn>
</Columns>
Default.aspx.cs 中:
public partial class _Default : System.Web.UI.Page
{
private string insertCommad = "Insert into stuInfo(stuNo,stuName,stuAddress) Values(@stuno,@stuname,@stuaddr)";
private string updateCommad = "update stuInfo set stuName=@stuname,stuAddress=@stuaddr where stuNo=@stuno";
private string deleteCommad = "delete from stuInfo where stuNo=@stuno";
private bool isAddNew = true;
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
BindData();
}
}
private void BindData()
{
BindData(false);
}
private void BindData(bool isAddNew)
{
SqlConnection cnn = new SqlConnection("server=.;uid=sa;pwd=;database=stuDB");
SqlDataAdapter adpt = new SqlDataAdapter("select * from stuInfo",cnn);
DataTable dt = new DataTable();
adpt.Fill(dt);
if (isAddNew)
{
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
}
DataGrid1.DataSource = dt;
if (isAddNew)
DataGrid1.EditItemIndex = dt.Rows.Count - 1;
DataBind();
}
private SqlCommand BuidCommand(string CommandText)
{
SqlConnection cnn = new SqlConnection("server=.;uid=sa;pwd=;database=stuDB");
SqlCommand command = new SqlCommand(CommandText, cnn);
command.CommandType = CommandType.Text;
cnn.Open();
return command;
}
private void Execute(string CommandText,DataGridCommandEventArgs e)
{
string stuno = ((TextBox)e.Item.FindControl("txtstuno")).Text;
string stuname = ((TextBox)e.Item.FindControl("txtstuname")).Text;
string stuaddr = Convert.ToString(((TextBox)e.Item.FindControl("txtstuaddr")).Text);
SqlParameter Sstuno = new SqlParameter("@stuno", SqlDbType.VarChar);
SqlParameter Sstuname = new SqlParameter("@stuname", SqlDbType.VarChar);
SqlParameter Sstuaddr = new SqlParameter("@stuaddr", SqlDbType.NVarChar);
Sstuno.Value = stuno;
Sstuname.Value = stuname;
Sstuaddr.Value = stuaddr;
if (stuno == "" && isAddNew == false)
{
isAddNew = true;
Execute(insertCommad,e);
return;
}
SqlCommand cmd = BuidCommand(CommandText);
if (!isAddNew)
{
cmd.Parameters.Add(Sstuno);
}
cmd.Parameters.Add(Sstuname);
cmd.Parameters.Add(Sstuaddr);
cmd.ExecuteNonQuery();
DataGrid1.EditItemIndex = -1;
}
protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
{
switch(e.CommandName.ToLower())
{
case "edit":
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindData();
break;
case "delete":
string stuno = ((Label)e.Item.FindControl("lblstuno")).Text;
SqlParameter Sstuno = new SqlParameter("@stuno", SqlDbType.VarChar);
Sstuno.Value = stuno;
SqlCommand cmd = BuidCommand(deleteCommad);
cmd.Parameters.Add(Sstuno);
cmd.ExecuteNonQuery();
DataGrid1.EditItemIndex = -1;
BindData();
break;
case "update":
Execute(updateCommad,e);
break;
case "cancel":
DataGrid1.EditItemIndex = -1;
BindData();
break;
case "insert":
BindData(true);
break;
}
}
}
[em4][em4][em4][em4][em4]
请各位老鸟们帮我看一下:
default.aspx中:
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label ID="lblstuno" runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.stuNo") %> '> </asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtstuno" runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.stuNo") %> '> </asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label ID="stuname1" runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.stuName") %> '> </asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtstuname" runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.stuName") %> '> </asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label ID="stuaddr1" runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.stuAddress") %> '> </asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtstuaddr" runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.stuAddress") %> '> </asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="Button1" runat="server" CommandName="btnaddnew" Text="AddNew" />
</FooterTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn CommandName="Select" Text="选择"> </asp:ButtonColumn>
<asp:EditCommandColumn CancelText="取消" EditText="编辑" UpdateText="更新"> </asp:EditCommandColumn>
<asp:ButtonColumn CommandName="Delete" Text="删除"> </asp:ButtonColumn>
</Columns>
Default.aspx.cs 中:
public partial class _Default : System.Web.UI.Page
{
private string insertCommad = "Insert into stuInfo(stuNo,stuName,stuAddress) Values(@stuno,@stuname,@stuaddr)";
private string updateCommad = "update stuInfo set stuName=@stuname,stuAddress=@stuaddr where stuNo=@stuno";
private string deleteCommad = "delete from stuInfo where stuNo=@stuno";
private bool isAddNew = true;
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
BindData();
}
}
private void BindData()
{
BindData(false);
}
private void BindData(bool isAddNew)
{
SqlConnection cnn = new SqlConnection("server=.;uid=sa;pwd=;database=stuDB");
SqlDataAdapter adpt = new SqlDataAdapter("select * from stuInfo",cnn);
DataTable dt = new DataTable();
adpt.Fill(dt);
if (isAddNew)
{
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
}
DataGrid1.DataSource = dt;
if (isAddNew)
DataGrid1.EditItemIndex = dt.Rows.Count - 1;
DataBind();
}
private SqlCommand BuidCommand(string CommandText)
{
SqlConnection cnn = new SqlConnection("server=.;uid=sa;pwd=;database=stuDB");
SqlCommand command = new SqlCommand(CommandText, cnn);
command.CommandType = CommandType.Text;
cnn.Open();
return command;
}
private void Execute(string CommandText,DataGridCommandEventArgs e)
{
string stuno = ((TextBox)e.Item.FindControl("txtstuno")).Text;
string stuname = ((TextBox)e.Item.FindControl("txtstuname")).Text;
string stuaddr = Convert.ToString(((TextBox)e.Item.FindControl("txtstuaddr")).Text);
SqlParameter Sstuno = new SqlParameter("@stuno", SqlDbType.VarChar);
SqlParameter Sstuname = new SqlParameter("@stuname", SqlDbType.VarChar);
SqlParameter Sstuaddr = new SqlParameter("@stuaddr", SqlDbType.NVarChar);
Sstuno.Value = stuno;
Sstuname.Value = stuname;
Sstuaddr.Value = stuaddr;
if (stuno == "" && isAddNew == false)
{
isAddNew = true;
Execute(insertCommad,e);
return;
}
SqlCommand cmd = BuidCommand(CommandText);
if (!isAddNew)
{
cmd.Parameters.Add(Sstuno);
}
cmd.Parameters.Add(Sstuname);
cmd.Parameters.Add(Sstuaddr);
cmd.ExecuteNonQuery();
DataGrid1.EditItemIndex = -1;
}
protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
{
switch(e.CommandName.ToLower())
{
case "edit":
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindData();
break;
case "delete":
string stuno = ((Label)e.Item.FindControl("lblstuno")).Text;
SqlParameter Sstuno = new SqlParameter("@stuno", SqlDbType.VarChar);
Sstuno.Value = stuno;
SqlCommand cmd = BuidCommand(deleteCommad);
cmd.Parameters.Add(Sstuno);
cmd.ExecuteNonQuery();
DataGrid1.EditItemIndex = -1;
BindData();
break;
case "update":
Execute(updateCommad,e);
break;
case "cancel":
DataGrid1.EditItemIndex = -1;
BindData();
break;
case "insert":
BindData(true);
break;
}
}
}
[em4][em4][em4][em4][em4]