請高手幫改寫一段datagrid程序. (100分相送)

nnh 2003-07-30 10:53:07
以下的這段程序是用 C#寫的,本人對C#不知,所以請高手幫把這段程序改寫成用VB寫的.謝謝.(100分相送)
<%@ Page Language="c#" debug="true"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">

//make first sql
String sql = "";
String strCnn = "Data Source=.;Initial Catalog=aa;User Id=sa;Password=;";
//create a datasource function
public ICollection CreateDataSource () {
SqlConnection conn = new SqlConnection(strCnn);

SqlDataAdapter db_sqladaptor = new SqlDataAdapter(sql,conn);

DataSet ds = new DataSet();
db_sqladaptor.Fill(ds,"MyDataResult");

DataView myView = ds.Tables["MyDataResult"].DefaultView;
return myView;
}


//do page load

public void Page_Load(Object sender, EventArgs e) {
strCnn = "Data Source=.;Initial Catalog=aa;User Id=sa;Password=;";

if (!IsPostBack)
{
sql = "Select * FROM People";
People.DataSource = CreateDataSource();
People.DataBind();
}

}

public void Page_Grid(Object sender, DataGridPageChangedEventArgs e)
{
sql = "Select * FROM People";
// Set CurrentPageIndex to the page the user clicked.
People.CurrentPageIndex = e.NewPageIndex;

// Rebind the data.
People.DataSource = CreateDataSource();
People.DataBind();

}

public void People_Edit(Object sender, DataGridCommandEventArgs e)
{
sql = "Select * FROM People";

People.EditItemIndex = e.Item.ItemIndex;
People.DataSource = CreateDataSource();
People.DataBind();

}

public void People_Cancel(Object sender, DataGridCommandEventArgs e)
{
sql = "Select * FROM People";
People.EditItemIndex = -1;
People.DataSource = CreateDataSource();
People.DataBind();

}

public void People_Update(Object sender, DataGridCommandEventArgs e)
{
string FirstName = ((TextBox)e.Item.Cells[1].Controls[1]).Text;
string LastName = ((TextBox)e.Item.Cells[2].Controls[1]).Text;

SqlConnection connUpdate = new SqlConnection(strCnn);
connUpdate.Open();
String sql_edit = "UPDATE People " +
"SET FirstName = '" + FirstName.Replace("'","''")+ "'," +
"LastName = '" + LastName.Replace("'","''")+ "'" +
" WHERE pkID = " + e.Item.Cells[0].Text;

SqlCommand sqlCommandUpdate = new SqlCommand(sql_edit,connUpdate);
sqlCommandUpdate.ExecuteNonQuery();
connUpdate.Close();

sql = "Select * FROM People";
People.EditItemIndex = -1;
People.DataSource = CreateDataSource();
People.DataBind();

}

public void People_Delete(Object sender, DataGridCommandEventArgs e) {

SqlConnection connDel = new SqlConnection(strCnn);
connDel.Open();
String sql_Del = "DELETE FROM People " +
" WHERE pkID = " + e.Item.Cells[0].Text;

SqlCommand sqlCommandDel = new SqlCommand(sql_Del,connDel);
sqlCommandDel.ExecuteNonQuery();
connDel.Close();

sql = "Select * FROM People";
People.EditItemIndex = -1;
People.DataSource = CreateDataSource();
People.DataBind();

}

</script>

<font face="arial" size="3">
<b>Edit People</b>
</font>
<br>

<form runat="server">

<asp:DataGrid id="People" runat="server"
BorderColor="green"
Width="640"
PageSize="5"
AllowPaging="true"
OnPageIndexChanged="Page_Grid"
BorderWidth="1"
CellPadding="3"
AutoGenerateColumns="false"
ShowHeader="true"
Visible="true"

OnEditCommand="People_Edit"
OnCancelCommand="People_Cancel"
OnUpdateCommand="People_Update"
OnDeleteCommand="People_Delete">

<HeaderStyle BorderColor="White" BackColor="black"
ForeColor="White"
Font-Bold="True"
Font-Name="Arial"
Font-Size="9" HorizontalAlign="Center"/>

<ItemStyle BorderColor=""
BackColor="#FFFFF0"
ForeColor="Black"
Font-Name="Arial"
Font-Size="8"
Font-Bold="False" HorizontalAlign="Center"/>

<EditItemStyle BorderColor=""
BackColor="#FFFFF0"
ForeColor="Black"
Font-Name="Arial"
Font-Size="7"
Font-Bold="False" HorizontalAlign="Center"/>

<PagerStyle Mode="NumericPages" Font-Size="8"/>

<Columns>

<asp:BoundColumn HeaderText="ID" ReadOnly="true" DataField="pkID"/>

<asp:TemplateColumn>

<HeaderTemplate>
<b> First Name </b>
</HeaderTemplate>

<ItemTemplate>
<asp:Label
Text='<%# DataBinder.Eval(Container.DataItem, "FirstName").ToString().Trim() %>'
runat="server"/>
</ItemTemplate>


<EditItemTemplate>
<asp:TextBox id="FirstName" Text='
<%# DataBinder.Eval(Container.DataItem, "FirstName").ToString().Trim() %>'
runat="server" Width="100%"/>
</EditItemTemplate>

</asp:TemplateColumn>

<asp:TemplateColumn>

<HeaderTemplate>
<b> Last Name </b>
</HeaderTemplate>

<ItemTemplate>
<asp:Label
Width="200"
Text='<%# DataBinder.Eval(Container.DataItem, "LastName").ToString().Trim() %>'
runat="server"/>
</ItemTemplate>

<EditItemTemplate>
<asp:TextBox id="LastName" Text='<br>
<%# DataBinder.Eval(Container.DataItem, "LastName").ToString().Trim() %>'
runat="server" Width="100%"/>
</EditItemTemplate>

</asp:TemplateColumn>

<asp:EditCommandColumn
ButtonType="LinkButton"
CancelText="Cancel"
EditText="Edit"
UpdateText="Update" />

<asp:ButtonColumn Text= "Delete" CommandName="Delete"></asp:ButtonColumn>

</Columns>

</asp:DataGrid>

</form>
...全文
23 8 打赏 收藏 举报
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
nnh 2003-07-31
  • 打赏
  • 举报
回复
沒人幫?或者你們有現成的也行呀.
nnh 2003-07-30
  • 打赏
  • 举报
回复
有沒有完整的,試運行無誤的程序呀.
hlj321 2003-07-30
  • 打赏
  • 举报
回复
up
接分
raingod 2003-07-30
  • 打赏
  • 举报
回复
up
  • 打赏
  • 举报
回复
代码部分:

Dim sql As [String] = ""
Dim strCnn As [String] = "Data Source=.;Initial Catalog=aa;User Id=sa;Password=;"

'create a datasource function
Public Function CreateDataSource() As ICollection
Dim conn As New SqlConnection(strCnn)

Dim db_sqladaptor As New SqlDataAdapter(sql, conn)

Dim ds As New DataSet()
db_sqladaptor.Fill(ds, "MyDataResult")

Dim myView As DataView = ds.Tables("MyDataResult").DefaultView
Return myView
End Function 'CreateDataSource



'do page load
Public Sub Page_Load(sender As [Object], e As EventArgs)
strCnn = "Data Source=.;Initial Catalog=aa;User Id=sa;Password=;"

If Not IsPostBack Then
sql = "Select * FROM People"
People.DataSource = CreateDataSource()
People.DataBind()
End If
End Sub 'Page_Load


Public Sub Page_Grid(sender As [Object], e As DataGridPageChangedEventArgs)
sql = "Select * FROM People"
' Set CurrentPageIndex to the page the user clicked.
People.CurrentPageIndex = e.NewPageIndex

' Rebind the data.
People.DataSource = CreateDataSource()
People.DataBind()
End Sub 'Page_Grid


Public Sub People_Edit(sender As [Object], e As DataGridCommandEventArgs)
sql = "Select * FROM People"

People.EditItemIndex = e.Item.ItemIndex
People.DataSource = CreateDataSource()
People.DataBind()
End Sub 'People_Edit


Public Sub People_Cancel(sender As [Object], e As DataGridCommandEventArgs)
sql = "Select * FROM People"
People.EditItemIndex = - 1
People.DataSource = CreateDataSource()
People.DataBind()
End Sub 'People_Cancel


Public Sub People_Update(sender As [Object], e As DataGridCommandEventArgs)
Dim FirstName As String = CType(e.Item.Cells(1).Controls(1), TextBox).Text
Dim LastName As String = CType(e.Item.Cells(2).Controls(1), TextBox).Text

Dim connUpdate As New SqlConnection(strCnn)
connUpdate.Open()
Dim sql_edit As [String] = "UPDATE People " + "SET FirstName = '" + FirstName.Replace("'", "''") + "'," + "LastName = '" + LastName.Replace("'", "''") + "'" + " WHERE pkID = " + e.Item.Cells(0).Text

Dim sqlCommandUpdate As New SqlCommand(sql_edit, connUpdate)
sqlCommandUpdate.ExecuteNonQuery()
connUpdate.Close()

sql = "Select * FROM People"
People.EditItemIndex = - 1
People.DataSource = CreateDataSource()
People.DataBind()
End Sub 'People_Update


Public Sub People_Delete(sender As [Object], e As DataGridCommandEventArgs)

Dim connDel As New SqlConnection(strCnn)
connDel.Open()
Dim sql_Del As [String] = "DELETE FROM People " + " WHERE pkID = " + e.Item.Cells(0).Text

Dim sqlCommandDel As New SqlCommand(sql_Del, connDel)
sqlCommandDel.ExecuteNonQuery()
connDel.Close()

sql = "Select * FROM People"
People.EditItemIndex = - 1
People.DataSource = CreateDataSource()
People.DataBind()
End Sub 'People_Delete
nnh 2003-07-30
  • 打赏
  • 举报
回复
up
szhuk 2003-07-30
  • 打赏
  • 举报
回复
也没有什么呀,自己试着改一下吧,主要不同的地方有定义参数和生成对象的语句
还有vb没有每句结束都用;
另外都是一些基本的语法,很容易的。
nnh 2003-07-30
  • 打赏
  • 举报
回复
up
发帖
.NET社区

6.1w+

社区成员

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

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

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