急!帮帮小妹!!:如何将数据库里面的记录以表格的形式输出,而且可以控制删除每一项记录?

winter2000 2003-10-16 09:57:13
问题如上!
留下msn,详谈!
...全文
116 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
nickwon 2003-12-26
  • 打赏
  • 举报
回复
我疯了,凭一个hotmail就得了60分,一大段源代码却只有30分!!MM好不公平哦!
fanli625 2003-10-16
  • 打赏
  • 举报
回复
无话可说了我。^_^
phpmysql 2003-10-16
  • 打赏
  • 举报
回复
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
<h2>Editable Data Grid
</h2>
<hr size="1" />
<form runat="server">
<asp:datagrid id="DataGrid1" runat="server" width="80%" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" ForeColor="Black" OnPageIndexChanged="DataGrid_Page" PageSize="6" AllowPaging="true" OnDeleteCommand="DataGrid_Delete" OnCancelCommand="DataGrid_Cancel" OnUpdateCommand="DataGrid_Update" OnEditCommand="DataGrid_Edit" OnItemCommand="DataGrid_ItemCommand" DataKeyField="au_id">
<HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
<PagerStyle horizontalalign="Right" backcolor="#C6C3C6" mode="NumericPages" font-size="smaller"></PagerStyle>
<ItemStyle backcolor="#DEDFDE"></ItemStyle>
<FooterStyle backcolor="#C6C3C6"></FooterStyle>
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit" ItemStyle-Font-Size="smaller" ItemStyle-Width="10%"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete" ItemStyle-Font-Size="smaller" ItemStyle-Width="10%"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
<br />
<asp:LinkButton id="LinkButton1" onclick="AddNew_Click" runat="server" Font-Size="smaller" Text="Add new item"></asp:LinkButton>
<br />
<br />
<asp:Label id="Message" runat="server" width="80%" forecolor="red" enableviewstate="false"></asp:Label>
</form>
</body>
</html>
phpmysql 2003-10-16
  • 打赏
  • 举报
回复
这是微软的范例看看吧~

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

// TODO: update the ConnectionString and Command values for your application

string ConnectionString = "server=(local);database=pubs;trusted_connection=true";
string SelectCommand = "SELECT au_id, au_lname, au_fname from Authors";

bool isEditing = false;

void Page_Load(object sender, EventArgs e) {

if (!Page.IsPostBack) {

// Databind the data grid on the first request only
// (on postback, bind only in editing, paging and sorting commands)

BindGrid();
}
}

// ---------------------------------------------------------------
//
// DataGrid Commands: Page, Sort, Edit, Update, Cancel, Delete
//

void DataGrid_ItemCommand(object sender, DataGridCommandEventArgs e) {

// this event fires prior to all of the other commands
// use it to provide a more graceful transition out of edit mode

CheckIsEditing(e.CommandName);
}

void CheckIsEditing(string commandName) {

if (DataGrid1.EditItemIndex != -1) {

// we are currently editing a row
if (commandName != "Cancel" && commandName != "Update") {

// user's edit changes (if any) will not be committed
Message.Text = "Your changes have not been saved yet. Please press update to save your changes, or cancel to discard your changes, before selecting another item.";
isEditing = true;
}
}
}

void DataGrid_Edit(object sender, DataGridCommandEventArgs e) {

// turn on editing for the selected row

if (!isEditing) {
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindGrid();
}
}

void DataGrid_Update(object sender, DataGridCommandEventArgs e) {

// update the database with the new values

// get the edit text boxes
string id = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
string lname = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
string fname = ((TextBox)e.Item.Cells[4].Controls[0]).Text;

// TODO: update the Command value for your application
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand UpdateCommand = new SqlCommand();
UpdateCommand.Connection = myConnection;

if (AddingNew)
UpdateCommand.CommandText = "INSERT INTO authors(au_id, au_lname, au_fname, contract) VALUES (@au_id, @au_lname, @au_fname, 0)";
else
UpdateCommand.CommandText = "UPDATE authors SET au_lname = @au_lname, au_fname = @au_fname WHERE au_id = @au_id";

UpdateCommand.Parameters.Add("@au_id", SqlDbType.VarChar, 11).Value = id;
UpdateCommand.Parameters.Add("@au_lname", SqlDbType.VarChar, 40).Value = lname;
UpdateCommand.Parameters.Add("@au_fname", SqlDbType.VarChar, 20).Value = fname;

// execute the command
try {
myConnection.Open();
UpdateCommand.ExecuteNonQuery();
}
catch (Exception ex) {
Message.Text = ex.ToString();
}
finally {
myConnection.Close();
}

// Resort the grid for new records
if (AddingNew) {

DataGrid1.CurrentPageIndex = 0;
AddingNew = false;
}

// rebind the grid
DataGrid1.EditItemIndex = -1;
BindGrid();
}

void DataGrid_Cancel(object sender, DataGridCommandEventArgs e) {

// cancel editing

DataGrid1.EditItemIndex = -1;
BindGrid();

AddingNew = false;
}

void DataGrid_Delete(object sender, DataGridCommandEventArgs e) {

// delete the selected row

if (!isEditing) {

// the key value for this row is in the DataKeys collection
string keyValue = (string)DataGrid1.DataKeys[e.Item.ItemIndex];

// TODO: update the Command value for your application
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand DeleteCommand = new SqlCommand("DELETE from authors where au_id='" + keyValue + "'", myConnection);

// execute the command
myConnection.Open();
DeleteCommand.ExecuteNonQuery();
myConnection.Close();

// rebind the grid
DataGrid1.CurrentPageIndex = 0;
DataGrid1.EditItemIndex = -1;
BindGrid();
}
}

void DataGrid_Page(object sender, DataGridPageChangedEventArgs e) {

// display a new page of data

if (!isEditing) {
DataGrid1.EditItemIndex = -1;
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
}

void AddNew_Click(Object sender, EventArgs e) {

// add a new row to the end of the data, and set editing mode 'on'

CheckIsEditing("");

if (!isEditing) {

// set the flag so we know to do an insert at Update time
AddingNew = true;

// add new row to the end of the dataset after binding

// first get the data
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter myCommand = new SqlDataAdapter(SelectCommand, myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds);

// add a new blank row to the end of the data
object[] rowValues = { "", "", "" };
ds.Tables[0].Rows.Add(rowValues);

// figure out the EditItemIndex, last record on last page
int recordCount = ds.Tables[0].Rows.Count;
if (recordCount > 1)
recordCount--;
DataGrid1.CurrentPageIndex = recordCount/DataGrid1.PageSize;
DataGrid1.EditItemIndex = recordCount%DataGrid1.PageSize;

// databind
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
}

// ---------------------------------------------------------------
//
// Helpers Methods:
//

// property to keep track of whether we are adding a new record,
// and save it in viewstate between postbacks

protected bool AddingNew {

get {
object o = ViewState["AddingNew"];
return (o == null) ? false : (bool)o;
}
set {
ViewState["AddingNew"] = value;
}
}

void BindGrid() {

SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter myCommand = new SqlDataAdapter(SelectCommand, myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds);

DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}

</script>
8u9 2003-10-16
  • 打赏
  • 举报
回复
直接读数据库,把数据循环输出就可以了啊。
页面上
<asp:table....>
<asp:TableRow>
<asp:TableCell...>
...
然后.cs文件里面通过DataReader读出数据
TableRow row=new TableRow();
TableCell cell=new TableCell;
cell.Text=myDataReader.GetValue(0).ToString();
row.Cells.Add(cell);
....
把你想要显示的内容循环显示就可以了

删除的话,我曾经是在每一个记录后边加上一个链接“删除”来进行的。

winter2000 2003-10-16
  • 打赏
  • 举报
回复
ft!!
为什么小弟可以搞计算机,小妹不行!?
acewang 2003-10-16
  • 打赏
  • 举报
回复
一分钱难倒英雄汉,我看我也别说了
nickwon 2003-10-16
  • 打赏
  • 举报
回复
小妹,你叫什么名字啊?美女为什么来搞计算机啊?
lbzq 2003-10-16
  • 打赏
  • 举报
回复
zhangqiang99@hotmail.com
winter2000 2003-10-16
  • 打赏
  • 举报
回复
ft!!到底有没有人帮忙呀!!
郁闷!
l_u_l_u 2003-10-16
  • 打赏
  • 举报
回复
你用datagrid 进行联接数据 、并配置它的del项
比尔咔咔 2003-10-16
  • 打赏
  • 举报
回复
2002pine(我学习,我存在)
见面再谈。


寒!!!!!
conichiwa 2003-10-16
  • 打赏
  • 举报
回复
请你先留下你的msn吧!^_^
kandyasp 2003-10-16
  • 打赏
  • 举报
回复
datagrid可以的

我不去见你了~~
2002pine 2003-10-16
  • 打赏
  • 举报
回复
见面再谈。
比尔咔咔 2003-10-16
  • 打赏
  • 举报
回复
.....
无话可说了

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

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

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

试试用AI创作助手写篇文章吧