DataGrid 可以不使用绑定数据原的方法使用吗,直接向Cell中写数据。

JCJC错别字检测-田春峰
博客专家认证
2003-01-03 04:29:43
DataGrid 可以不使用绑定数据原的方法使用吗,直接向Cell中写数据。

我的意思是:不是用DataSource , 直接向其中的 cell 写数据

然后手动维护其中的数据变化!

如果可以请给个例子好吗?
如果不可以为什么?
...全文
133 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Soking 2003-05-19
  • 打赏
  • 举报
回复
DataGrid的好处也就在于绑定,不过你可以手工建立一个
DataSet 而不要连接到数据库阿!
chsl918 2003-05-19
  • 打赏
  • 举报
回复
同意chinchy(人民需要人民币)。
其实是一样的,你只要帮定,什么不同的。
以下程序存成aspx文件。放到IIS运行看一下就知道了。
本程序出自MSDN中。
<%@ Import Namespace="System.Data" %>

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

DataTable Cart;
DataView CartView;

ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();

dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i + 1);

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}

void Page_Load(Object sender, EventArgs e)
{

if (Session["DG4_ShoppingCart"] == null)
{
Cart = new DataTable();
Cart.Columns.Add(new DataColumn("Item", typeof(string)));
Cart.Columns.Add(new DataColumn("Price", typeof(string)));
Session["DG4_ShoppingCart"] = Cart;
}

else
{
Cart = (DataTable)Session["DG4_ShoppingCart"];
}

CartView = new DataView(Cart);
ShoppingCart.DataSource = CartView;
ShoppingCart.DataBind();

if (!IsPostBack)
{
// Load this data only once.
ItemsGrid.DataSource= CreateDataSource();
ItemsGrid.DataBind();
}
}

void Grid_CartCommand(Object sender, DataGridCommandEventArgs e)
{

DataRow dr = Cart.NewRow();

// e.Item is the table row where the command is raised.
// For bound columns, the value is stored in the Text property of the TableCell.
TableCell itemCell = e.Item.Cells[2];
TableCell priceCell = e.Item.Cells[3];
string item = itemCell.Text;
string price = priceCell.Text;

if (((Button)e.CommandSource).CommandName == "AddToCart")
{
dr[0] = item;
dr[1] = price;
Cart.Rows.Add(dr);
}

else
{

// Remove from Cart.

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

ShoppingCart.DataBind();

}


</script>

<body>

<form runat=server>

<h3>DataGrid Example</h3>

<table cellpadding="5">
<tr valign="top">
<td>

<b>Product List</b>

<asp:DataGrid id="ItemsGrid"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
AutoGenerateColumns="false"
OnItemCommand="Grid_CartCommand"
runat="server">

<HeaderStyle BackColor="#00aaaa">
</HeaderStyle>

<Columns>

<asp:ButtonColumn
HeaderText="Add to cart"
ButtonType="PushButton"
Text="Add"
CommandName="AddToCart" />

<asp:ButtonColumn
HeaderText="Remove from cart"
ButtonType="PushButton"
Text="Remove"
CommandName="RemoveFromCart" />

<asp:BoundColumn
HeaderText="Item"
DataField="StringValue"/>

<asp:BoundColumn
HeaderText="Price"
DataField="CurrencyValue"
DataFormatString="{0:c}">

<ItemStyle HorizontalAlign="right">
</ItemStyle>

</asp:BoundColumn>

</Columns>

</asp:DataGrid>

</td>
<td>

<b>Shopping Cart</b>

<asp:DataGrid id="ShoppingCart"
runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
ShowFooter="false"
CellPadding="3"
CellSpacing="0">

<HeaderStyle BackColor="#00aaaa">
</HeaderStyle>

</asp:DataGrid>

</td>
</tr>

</table>

</form>

</body>
</html>
yqdeng 2003-05-19
  • 打赏
  • 举报
回复
如果象这样用DataGrid可能就有点自找麻烦了,没必要:-)
chinchy 2003-05-19
  • 打赏
  • 举报
回复
那你直接用table不是更好
顾君彦 2003-05-19
  • 打赏
  • 举报
回复
DataGrid[x,y]="sdfafd"

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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