急!GridView绑定数据库查询的数据,绑定的字段重复显示怎么修改?

lxbgg 2012-09-05 08:04:16
我想绑定我想要的两个字段,但是先绑定了字段,然后再把数据库里查询的字段全部绑定,不知道怎么修改,高手帮帮忙啊

后台代码

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OracleClient;

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
fillGridView();
}

}
//填充GridView
protected void fillGridView()
{
DataSet ds = new DataSet();

ds = GetData();
GridView1.DataSource = ds;
GridView1.DataBind();
getRefSet(ds);

}
//分页
protected void getRefSet(DataSet ds)
{
lblCurrentIndex.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 页";
lblPageCount.Text = "共 " + GridView1.PageCount.ToString() + " 页";
lblRecordCount.Text = "总共 " + ds.Tables[0].Rows.Count.ToString() + " 条";

if (ds.Tables[0].Rows.Count == 0)
{
lnkbtnFirst.Visible = false;
lnkbtnPre.Visible = false;
lnkbtnNext.Visible = false;
lnkbtnLast.Visible = false;

lblCurrentIndex.Visible = false;
lblPageCount.Visible = false;
lblRecordCount.Visible = false;
}
else if (GridView1.PageCount == 1)
{
lnkbtnFirst.Visible = false;
lnkbtnPre.Visible = false;
lnkbtnNext.Visible = false;
lnkbtnLast.Visible = false;
}
lnkbtnFirst.CommandArgument = "1";
lnkbtnPre.CommandArgument = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());
lnkbtnNext.CommandArgument = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
lnkbtnLast.CommandArgument = GridView1.PageCount.ToString();
}
//第几页
protected void lnkbtnJumpPage_Click(object sender, EventArgs e)
{
GridView1.PageIndex = int.Parse(txtJumpPage.Text) - 1;
lblCurrentIndex.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 页";
fillGridView();
GridView1.DataBind();

}
protected void PagerButtonClick(object sender, EventArgs e)
{
GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandArgument) - 1;
fillGridView();
GridView1.DataBind();

}
// 转换SQL查询结果为DataSet
private DataSet GetData()
{

OracleConnection conn = new OracleConnection("Data Source=wgyx;user=wgyx;password=wgyx;");
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
String strSql = "select id,fuid,the from sys_organ";
cmd.CommandText = strSql;
cmd.CommandType = CommandType.Text;
DataSet ds = new DataSet();
try
{
conn.Open();
OracleDataAdapter ad = new OracleDataAdapter();
ad.SelectCommand = cmd;
ad.Fill(ds);

}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
finally
{
conn.Close();
}
return ds;
}
}


前台代码

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" >
<AlternatingRowStyle BackColor="LightSteelBlue" />
<PagerSettings Visible="False" />
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<%# Eval("id"%>
</ItemTemplate>
<ItemStyle BorderColor="LightGray" BorderStyle="Double" BorderWidth="1px" Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="地名">
<ItemTemplate>
<%# Eval("the")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


...全文
270 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxbgg 2012-09-06
  • 打赏
  • 举报
回复

完全正确,谢谢
[Quote=引用 3 楼 的回复:]
"设计"页面,去掉gridview的"自动产生列"的功能。
[/Quote]
xiedu414 2012-09-05
  • 打赏
  • 举报
回复
"设计"页面,去掉gridview的"自动产生列"的功能。
guijingcun 2012-09-05
  • 打赏
  • 举报
回复
建议你找个前端,帮你写个样式,用Repter吧
EnForGrass 2012-09-05
  • 打赏
  • 举报
回复
用BoundField

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" Height="285px"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" Width="771px">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="id" HeaderText="学号" />
<asp:BoundField DataField="name" HeaderText="姓名" />
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:BoundField DataField="age" HeaderText="年龄" />
<asp:BoundField DataField="department" HeaderText="专业" />
<asp:BoundField DataField="grade" HeaderText="班级" />
<asp:CommandField HeaderText="编辑 " ShowEditButton="True" />
<asp:CommandField HeaderText="选择" ShowSelectButton="True" />
<asp:TemplateField HeaderText="删除 " ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Delete" Text="删除" OnClientClick="return confirm('你确定要删除吗?')"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>

参考http://wangfeng5271.blog.163.com/blog/static/4817444420113132358504/

62,254

社区成员

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

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

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

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