C# 如何使用asp.net和c从不同的表中创建具有自定义列的网格视图?
现在,我对如何使用以下结构和条件创建gridview感到非常困惑: 注意:gridview将从不同的表中获取数据 栏目: 1.IDcol是唯一的值,它将来自tableAas文本 2.日期也将从表格文本开始 3.link1它将是指向另一个页面的超链接,url的参数将是“IDcol”列值,但如果此记录存在于具有相同“IDcol”的表B中,则显示的文本将更改。如果不存在,则显示的内容将被查看/编辑。如果不存在,则将添加新内容 数据库结构: 表a: 表B: 所以我需要使用循环填充gridview,因为我必须检查每一行并使用一些条件 对不起,如果我的描述方式不清楚,但我真的很困惑 删除部件的我的代码:C# 如何使用asp.net和c从不同的表中创建具有自定义列的网格视图?,c#,asp.net,gridview,C#,Asp.net,Gridview,现在,我对如何使用以下结构和条件创建gridview感到非常困惑: 注意:gridview将从不同的表中获取数据 栏目: 1.IDcol是唯一的值,它将来自tableAas文本 2.日期也将从表格文本开始 3.link1它将是指向另一个页面的超链接,url的参数将是“IDcol”列值,但如果此记录存在于具有相同“IDcol”的表B中,则显示的文本将更改。如果不存在,则显示的内容将被查看/编辑。如果不存在,则将添加新内容 数据库结构: 表a: 表B: 所以我需要使用循环填充gridview,因为我
当我点击delete link删除行时,它会工作,但当刷新页面时,它会删除另一行,而不点击delete link,那么为什么会发生这种情况???从两个表创建一个数据视图,并将网格绑定到它。在呈现GridView时,在行事件(即rowbound)中,您可以设置条件。如果我没有弄错,您需要从多个表中选择一些列,如A、B、C,然后在GridView中显示它们。如果是的话,可以通过各种方式来实现。 1.创建包含所需数据列的自定义数据集,并使用select查询填充该数据集。然后将数据集绑定到gridview中。 2.创建一个包含所需数据列的数据表,并使用select查询填充它。然后将表绑定到gridview中。 3.将所需的列直接添加到gridview中,然后使用循环在其中添加数据项。
注意:在任何情况下,请取消选中gridview的AutoGenerateColumn选项以实现平滑操作。无论在gridview中使用多少表来显示数据,最好且更灵活的方法是使用OnRowDataBound
//do some database queries to return the appropriate value
DataTable dt = new DataTable();
private void Bind()
{
//set up the dt with all the required data from single or multiple tables
}
//Then in the page Load method, call the above Bind method
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
//then do something like the following
int idx = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//do some database queries to return the appropriate value, then do something like the following
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = dt.Rows[idx][0];
e.Row.Cells[1].Text = dt.Rows[idx][1];
//or maybe
((TextBox)e.Row.Cells[0].FindControl("textBox1")).Text = dt.Rows[idx][0];
((Label)e.Row.Cells[1].FindControl("label2")).Text = dt.Rows[idx][1];
((CheckBox)e.Row.Cells[1].FindControl("chkbx1")).Selected = (bool)dt.Rows[idx][2];
idx++;
}
}
正如Pranay所说,您可能希望使用Linq和join从所有表中获取所需的数据,但是,您仍然可以通过任何其他方式获取这些数据,您只需一个建议即可,为什么不在需要循环和检查某些条件时将所有列放入一个表中呢?因为我只需要为用户显示特定的列,这将取决于数据库中来自多个表的数据,而不仅仅是一个表,这就是我的意思,当你根据条件循环时,将所需的值添加到一个主表中表您可以在尽可能多的表中查找数据,然后将数据推送到所需的单元格中。希望您能够修复要显示的列mm我想我会尝试您的建议并告诉您:thanks@speciallife-你有没有试过一些正常的东西是的,你是对的,我想试一下你的第三个建议,因为它会满足我的需要检查一些条件谢谢你的回答我用sqldatasource做了,然后对OnRowDataBound中的内容进行了编辑,你的建议也很好谢谢:但是我现在在刷新页面时delete命令有另一个问题OnRowDeleting事件再次触发,删除另一行而不单击delete链接我自己,你能帮我一下吗?好的,我会给你看我这个页面的代码,也许我犯了一个大错误:现在再次检查我的问题,请我添加要删除的部分:提前谢谢我想知道你是如何和在哪里绑定数据的?!如何执行删除?我注意到您说您正在使用SqlDataSource,如果是这样,删除应该非常简单,只需将DeleteCommand添加到SqlDataSource&不需要OnDelete或OnDeleted。让我知道结果。如果这解决了您的问题,请不要忘记将问题标记为已回答。根本不需要GridView1\u行删除,如果您愿意,可以将其保留为空代码。只需添加CommandDelete和CommandField,如我之前的评论或链接按钮所示,将UserName=@UserName替换为ID=@ID
ID,
IDcol as (foreign key from tableA).
other fields
<asp:LinkButton ID="DeleteLink" runat="server" Text="Delete" CommandName="Delete"></asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
DeleteCommand="DELETE VisitsInfo WHERE ID=@VID">
<DeleteParameters>
<asp:Parameter Name="VID" Type="Int64" />
</DeleteParameters>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int VID = int.Parse(GridView1.DataKeys[0].Value.ToString());
SqlDataSourceVisits.DeleteParameters[0].DefaultValue = VID.ToString();
}
//do some database queries to return the appropriate value
DataTable dt = new DataTable();
private void Bind()
{
//set up the dt with all the required data from single or multiple tables
}
//Then in the page Load method, call the above Bind method
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
//then do something like the following
int idx = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//do some database queries to return the appropriate value, then do something like the following
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = dt.Rows[idx][0];
e.Row.Cells[1].Text = dt.Rows[idx][1];
//or maybe
((TextBox)e.Row.Cells[0].FindControl("textBox1")).Text = dt.Rows[idx][0];
((Label)e.Row.Cells[1].FindControl("label2")).Text = dt.Rows[idx][1];
((CheckBox)e.Row.Cells[1].FindControl("chkbx1")).Selected = (bool)dt.Rows[idx][2];
idx++;
}
}