各位可以提供datagrid编辑删除的代码吗,vb的

a791223 2003-07-24 06:54:23
谢谢大家了
...全文
25 11 打赏 收藏 举报
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
a791223 2003-07-25
  • 打赏
  • 举报
回复
代码如下:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.sqlclient"%>
<html>

<script language="VB" runat="server">

Dim CartView As DataView
Dim runningTotal As Double = 0

'Cart 是 Page 上的一个属性
'ReadOnly Property Cart As DataTable
' Get
' Dim tmpCart As DataTable
' Dim i As Integer
' Dim dr As DataRow
'
' If Session("DG_ShoppingCart") Is Nothing Then
' tmpCart = new DataTable()
' tmpCart.Columns.Add(new DataColumn("数量", GetType(String)))
' tmpCart.Columns.Add(new DataColumn("产品", GetType(String)))
' tmpCart.Columns.Add(new DataColumn("价格", GetType(Double)))
' tmpCart.Columns.Add(new DataColumn("礼品包", GetType(Boolean)))
' Session("DG_ShoppingCart") = tmpCart

' 第一次加载 -- 预填充一些数据
' For i= 1 to 6
' dr = tmpCart.NewRow()
' dr(0) = "1"
' dr(1) = "产品" & i.ToString
' dr(2) = 1.23 * (i+1)
' dr(3) = false
' tmpCart.Rows.Add(dr)
' Next
' Return tmpCart
' Else
' Return Session("DG_ShoppingCart")
' End If
'End Get
'End Property

'Sub Page_Init(sender As Object, e As EventArgs)
' MyDataGrid.EnableViewState = true
'End Sub

Sub Page_Load(sender As Object, e As EventArgs)
'CartView = Cart.DefaultView
If Not IsPostBack Then
BindGrid
End If
End Sub

'Sub BindGrid()
' MyDataGrid.DataSource = CartView
' MyDataGrid.DataBind()
' End Sub


Sub BindGrid()
Dim objconnection1 As SqlConnection '定义连接
Dim strConnection1 As String = "user id=sa;password=1979;database=Northwind;server=192.168.0.253;connect Timeout=30"
objconnection1 = New SqlConnection(strConnection1) '引用
Dim strsql As String = "select * from customers"
Dim objDataSet As New DataSet()
Dim objadapter As New SqlDataAdapter(strsql, objconnection1)
objadapter.Fill(objDataSet, "testtable")
Dim objtable As DataTable
objtable = objDataSet.Tables("testtable")
Dim objDataView As New DataView(objDataSet.Tables("testtable"))

MyDataGrid.DataSource = objDataView
MyDataGrid.DataBind()
End Sub

Sub btnUpdate_click(sender As Object, e As EventArgs)
Dim i As Integer
Dim _item As DataGridItem
Dim dr As DataRow

For i = 0 To MyDataGrid.Items.Count - 1

_item = MyDataGrid.Items(i)
Dim qtyTextBox As TextBox= _item.FindControl("txtQty")
Dim giftCheckBox As CheckBox = _item.FindControl("chkGIft")

' 对于数据库,我们应使用一条更新命令。
' 因为这是一个内存内数据表,所以我们只更改内存内的行。
' dr = Cart.Rows(i)
' dr(0) = qtyTextBox.Text
' dr(3) = giftCheckBox.Checked
Next
BindGrid
End Sub


' Function CalcTotal (count As Integer, price As Double) As Double
' Dim total As Double
'
' total = count * price
' runningTotal += total
'
' CalcTotal = total
' End Function

</script>

<body>

<h3><font face="宋体">DataGrid 的自定义编辑</font></h3>

<form runat=server ID="Form1">

<ASP:DataGrid id="MyDataGrid" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="none"
CellPadding="4"
Font-Name="宋体"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="false"
>
<Columns>
<asp:TemplateColumn HeaderText="companyname">
<ItemTemplate>
<asp:TextBox id=txtQty runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "testint") %>'
Width="40px"
/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="companyname" DataField="companyname"/>
<asp:BoundColumn HeaderText="companyname" DataField="companyname" DataFormatString="{0:c}" />
<asp:TemplateColumn HeaderText="testint">
<ItemTemplate>
<center>
<asp:CheckBox id=chkGIft runat="server"
Checked='<%# DataBinder.Eval(Container.DataItem, "testint") %>'
/>
</center>
</ItemTemplate>
</asp:TemplateColumn>

</Columns>

</asp:DataGrid>

<asp:LinkButton id=btnUpdate runat="server"
Text="更新合计"
Font-Name="Verdana"
Font-Size="9pt"
onClick="btnUpdate_click"
/>

</form>

</body>
</html>
a791223 2003-07-25
  • 打赏
  • 举报
回复
我现在可以运行了,但是我提交更新是那要如何操作数据库呢。
a791223 2003-07-25
  • 打赏
  • 举报
回复
可以提供编辑的代码例子吗,谢谢了。调用数据库的示例,初学net请大家帮助万分感谢
007Delphi 2003-07-24
  • 打赏
  • 举报
回复
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>


<script language="VB" runat="server">

Dim MyConnection As SqlConnection

Sub Page_Load(Sender As Object, E As EventArgs)

MyConnection = New SqlConnection("server=(local)\NetSDK;database=pubs;Trusted_Connection=yes")

If Not (IsPostBack)
BindGrid()
End If
End Sub

Sub MyDataGrid_Delete(Sender As Object, E As DataGridCommandEventArgs)

Dim MyCommand As SqlCommand
Dim DeleteCmd As String = "DELETE from Employee where emp_id = @Id"

MyCommand = New SqlCommand(DeleteCmd, MyConnection)
MyCommand.Parameters.Add(New SqlParameter("@Id", SqlDbType.NVarChar, 11))
MyCommand.Parameters("@Id").Value = MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))

MyCommand.Connection.Open()

Try
MyCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>已删除记录</b><br>" & DeleteCmd
Catch Exc As SQLException
Message.InnerHtml = "错误:未能删除记录"
Message.Style("颜色") = "红色"
End Try

MyCommand.Connection.Close()

BindGrid()
End Sub

Sub BindGrid()

Dim DS As DataSet
Dim MyCommand As SqlDataAdapter
MyCommand = New SqlDataAdapter("select * from Employee", MyConnection)

DS = new DataSet()
MyCommand.Fill(DS, "员工")

MyDataGrid.DataSource=DS.Tables("员工").DefaultView
MyDataGrid.DataBind()
End Sub

</script>

<body style="font: 10.5pt 宋体">

<form runat="server">

<h3><font face="宋体">删除数据行</font></h3>

<span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/><p>

<ASP:DataGrid id="MyDataGrid" runat="server"
Width="800"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
DataKeyField="emp_id"
OnDeleteCommand="MyDataGrid_Delete"
>

<Columns>
<asp:ButtonColumn Text="删除员工" CommandName="Delete"/>
</Columns>

</ASP:DataGrid>

</form>

</body>
</html>
得自http://chs.gotdotnet.com/quickstart/aspplus/
ruyedian 2003-07-24
  • 打赏
  • 举报
回复
把这句去掉就能正常显示了,我试过了
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm6.aspx.vb" Inherits="eOMPWebMe.WebForm6"%>
ruyedian 2003-07-24
  • 打赏
  • 举报
回复
把这个去掉:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm6.aspx.vb" Inherits="eOMPWebMe.WebForm6"%>
去掉后我这里能运行的,一切正常
a791223 2003-07-24
  • 打赏
  • 举报
回复
有人知道吗
a791223 2003-07-24
  • 打赏
  • 举报
回复
为什么以上代码我运行时什么显示也没有,datagrid一片空白
a791223 2003-07-24
  • 打赏
  • 举报
回复
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm6.aspx.vb" Inherits="eOMPWebMe.WebForm6"%>
<%@ Import Namespace="System.Data" %>

<html>

<script language="VB" runat="server">

Dim CartView As DataView
Dim runningTotal As Double = 0

'Cart 是 Page 上的一个属性
ReadOnly Property Cart As DataTable
Get
Dim tmpCart As DataTable
Dim i As Integer
Dim dr As DataRow

If Session("DG_ShoppingCart") Is Nothing Then
tmpCart = new DataTable()
tmpCart.Columns.Add(new DataColumn("数量", GetType(String)))
tmpCart.Columns.Add(new DataColumn("产品", GetType(String)))
tmpCart.Columns.Add(new DataColumn("价格", GetType(Double)))
tmpCart.Columns.Add(new DataColumn("礼品包", GetType(Boolean)))
Session("DG_ShoppingCart") = tmpCart

' 第一次加载 -- 预填充一些数据
For i= 1 to 6
dr = tmpCart.NewRow()
dr(0) = "1"
dr(1) = "产品" & i.ToString
dr(2) = 1.23 * (i+1)
dr(3) = false
tmpCart.Rows.Add(dr)
Next
Return tmpCart
Else
Return Session("DG_ShoppingCart")
End If
End Get
End Property

'Sub Page_Init(sender As Object, e As EventArgs)
' MyDataGrid.EnableViewState = true
'End Sub

Sub Page_Load(sender As Object, e As EventArgs)
CartView = Cart.DefaultView
If Not IsPostBack Then
BindGrid
End If
End Sub

Sub BindGrid()
MyDataGrid.DataSource = CartView
MyDataGrid.DataBind()
End Sub

Sub btnUpdate_click(sender As Object, e As EventArgs)
Dim i As Integer
Dim _item As DataGridItem
Dim dr As DataRow

For i = 0 To MyDataGrid.Items.Count - 1

_item = MyDataGrid.Items(i)
Dim qtyTextBox As TextBox= _item.FindControl("txtQty")
Dim giftCheckBox As CheckBox = _item.FindControl("chkGIft")

' 对于数据库,我们应使用一条更新命令。
' 因为这是一个内存内数据表,所以我们只更改内存内的行。
dr = Cart.Rows(i)
dr(0) = qtyTextBox.Text
dr(3) = giftCheckBox.Checked
Next
BindGrid
End Sub


Function CalcTotal (count As Integer, price As Double) As Double
Dim total As Double

total = count * price
runningTotal += total

CalcTotal = total
End Function

</script>

<body>

<h3><font face="宋体">DataGrid 的自定义编辑</font></h3>

<form runat=server ID="Form1">

<ASP:DataGrid id="MyDataGrid" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="none"
CellPadding="4"
Font-Name="宋体"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="false"
>
<Columns>
<asp:TemplateColumn HeaderText="数量">
<ItemTemplate>
<asp:TextBox id=txtQty runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "数量") %>'
Width="40px"
/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="产品" DataField="产品"/>
<asp:BoundColumn HeaderText="价格" DataField="价格" DataFormatString="{0:c}" />
<asp:TemplateColumn HeaderText="礼品包?">
<ItemTemplate>
<center>
<asp:CheckBox id=chkGIft runat="server"
Checked='<%# DataBinder.Eval(Container.DataItem, "礼品包") %>'
/>
</center>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="小计">
<ItemTemplate>
<p align="right">
<asp:Label runat="server"
Text='<%# System.String.Format("{0:c}",CalcTotal(Int32.Parse(DataBinder.Eval(Container.DataItem, "数量")),DataBinder.Eval(Container.DataItem, "价格"))) %>'
ID="Label1" NAME="Label1"/>
</p>
</ItemTemplate>
<FooterTemplate>
<p align="right"><b>
<asp:Label runat="server"
Text='<%# System.String.Format("{0:c}", runningTotal) %>'
ID="Label2" NAME="Label2"/>
</b></p>
</FooterTemplate>

</asp:TemplateColumn>
</Columns>

</asp:DataGrid>

<asp:LinkButton id=btnUpdate runat="server"
Text="更新合计"
Font-Name="Verdana"
Font-Size="9pt"
onClick="btnUpdate_click"
/>

</form>

</body>
</html>
chenee543216 2003-07-24
  • 打赏
  • 举报
回复
<%@ Import Namespace="System.Data" %>

<html>

<script language="C#" runat="server">

DataTable Cart = new DataTable();
DataView CartView;

void Page_Load(Object sender, EventArgs e)
{
DataRow dr;

if (Session["ShoppingCart"] == null)
{
Cart.Columns.Add(new DataColumn("Qty", typeof(String)));
Cart.Columns.Add(new DataColumn("Item", typeof(String)));
Cart.Columns.Add(new DataColumn("Price", typeof(String)));
Session["ShoppingCart"] = Cart;

// Create rows and add sample data.
for (int i = 1; i <= 9; i++)
{
dr = Cart.NewRow();
if (i % 2 != 0)
dr[0] = "2";
else
dr[0] = "1";
dr[1] = "Item " + i.ToString();
dr[2] = (1.23 * (i + 1)).ToString();
Cart.Rows.Add(dr);
}
}
else
Cart = (DataTable)Session["ShoppingCart"];

CartView = new DataView(Cart);
CartView.Sort="Item";

if (!IsPostBack)
BindGrid();

}

void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e)
{
MyDataGrid.EditItemIndex = e.Item.ItemIndex;
BindGrid();
}

void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
}

void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)
{
// For bound columns, the edited value is stored in a TextBox.
// The TextBox is the 0th element in the column's cell.
TextBox qtyText = (TextBox)e.Item.Cells[3].Controls[0];
TextBox priceText = (TextBox)e.Item.Cells[4].Controls[0];

String item = e.Item.Cells[2].Text;
String qty = qtyText.Text;
String price = priceText.Text;

DataRow dr;

// With a database, use an update command to update the data. Because
// the data source in this example is an in-memory DataTable, delete the
// old row and replace it with a new one.

// Remove old entry.
CartView.RowFilter = "Item='" + item + "'";
if (CartView.Count > 0)
CartView.Delete(0);
CartView.RowFilter = "";

// Add new entry.
dr = Cart.NewRow();
dr[0] = qty;
dr[1] = item;
dr[2] = price;
Cart.Rows.Add(dr);

MyDataGrid.EditItemIndex = -1;
BindGrid();
}

void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs e)
{
DataRow dr;

String item = e.Item.Cells[2].Text;

CartView.RowFilter = "Item='" + item + "'";
if (CartView.Count > 0)
CartView.Delete(0);
CartView.RowFilter = "";

BindGrid();
}

void BindGrid()
{
MyDataGrid.DataSource = CartView;
MyDataGrid.DataBind();
}

</script>

<body>

<form runat="server">

<h3>DataGrid Editing Example</h3>

<asp:DataGrid id="MyDataGrid" runat="server"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
Font-Name="Verdana"
Font-Size="8pt"
OnEditCommand="MyDataGrid_Edit"
OnCancelCommand="MyDataGrid_Cancel"
OnUpdateCommand="MyDataGrid_Update"
OnDeleteCommand="MyDataGrid_Delete"
AutoGenerateColumns="false">

<HeaderStyle BackColor="#aaaadd">
</HeaderStyle>

<Columns>
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
HeaderText="Edit Command Column">

<ItemStyle Wrap="false">
</ItemStyle>

<HeaderStyle Wrap="false">
</HeaderStyle>

</asp:EditCommandColumn>

<asp:ButtonColumn
HeaderText="Delete Item"
ButtonType="LinkButton"
Text="Delete"
CommandName="Delete">

</asp:ButtonColumn>

<asp:BoundColumn HeaderText="Item" ReadOnly="true" DataField="Item"/>
<asp:BoundColumn HeaderText="Quantity" DataField="Qty"/>
<asp:BoundColumn HeaderText="Price" DataField="Price"/>
</Columns>
</asp:DataGrid>

</form>

</body>
</html>
'MS的帮助文档
xrll 2003-07-24
  • 打赏
  • 举报
回复
quickstart的例子中有现成的呀!
发帖
.NET社区

6.1w+

社区成员

.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
帖子事件
创建了帖子
2003-07-24 06:54
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。