GridView 处理行数据,诡异的问题啊

S_zxing 2011-06-09 09:52:16
用GridView在后台绑定数据库,并将相应行中相应列数据处理一下,不过就是显示不出来处理后的数据,诡异啊,以前是可以实现这样的,唉,我不知道是哪里出问题了,请帮忙看看,不胜感激!下面是代码……
前台代码:
其中要处理的是,teacherID,courseID,Point,SinOrDou

<asp:GridView ID="GVStuClass" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" CssClass="gridviewText"
AllowPaging="True"
AllowSorting="True">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<Columns>
<asp:BoundField DataField="ClassID" Visible="False" />
<asp:BoundField DataField="CourseID" HeaderText="上什么课" >
<ItemStyle Width="15%" />
</asp:BoundField>
<asp:BoundField DataField="TeacherID" HeaderText="授课教师" >
<ItemStyle Width="15%" />
</asp:BoundField>
<asp:BoundField DataField="ClassPlace" HeaderText="上课地点" >
<ItemStyle Width="13%" />
</asp:BoundField>
<asp:BoundField DataField="ClassTime" HeaderText="上课时间" >
<ItemStyle Width="13%" />
</asp:BoundField>
<asp:BoundField DataField="Point" HeaderText="学分" >
<ItemStyle Width="7%" />
</asp:BoundField>
<asp:BoundField DataField="SinOrDou" HeaderText="单/双周" >
<ItemStyle Width="12%" />
</asp:BoundField>
<asp:BoundField DataField="DepartID" HeaderText="所属学院" >
<ItemStyle Width="18%" />
</asp:BoundField>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CBStuClass" runat="server" />
</ItemTemplate>
<ItemStyle Width="8%" />
</asp:TemplateField>
</Columns>
</asp:GridView>


后台代码:
后台处理teacherID,courseID,Point,SinOrDou这些列,并用其他相关信息在GridView是显示出来

protected void Page_Load(object sender, EventArgs e)
{
string userName = Session["userName"].ToString();
AdminBLL adminObject = new AdminBLL();
departID = Convert.ToInt32(adminObject.GetAdminByAdminName(userName).Rows[0]["AdminDepartID"].ToString());

if (!IsPostBack)
{
GVStuClassBind();

}
}

// GridView绑定数据库
public void GVStuClassBind()
{
string adminName = Session["userName"].ToString();
ClassBLL classObject = new ClassBLL();
CourseBLL courseObject = new CourseBLL();
TeacherBLL teacherObject = new TeacherBLL();
DepartBLL departObject = new DepartBLL();
AdminBLL adminObject = new AdminBLL();

GVStuClass.DataSource = classObject.GetClassByDepartID(departID);
GVStuClass.DataBind();

for (int i = 0; i < GVStuClass.Rows.Count; i++)
{
string teacherName, courseName, departName;
int SinOrDou;

int teacherID = Convert.ToInt32(classObject.GetClassByDepartID(departID).Rows[i]["teacherID"].ToString());
teacherName = teacherObject.GetTeacherByTeacherID(teacherID).Rows[0]["TeacherName"].ToString();

int courseID = Convert.ToInt32(classObject.GetClassByDepartID(departID).Rows[i]["CourseID"].ToString());
courseName = courseObject.GetCourseByCourseID(courseID).Rows[0]["CourseName"].ToString();


departName = departObject.GetDepartByDepartID(departID).Rows[0]["DepartName"].ToString();
SinOrDou = Convert.ToInt32(classObject.GetClassByDepartID(departID).Rows[i]["SinOrDou"].ToString());

if (SinOrDou == 1)
GVStuClass.Rows[i].Cells[6].Text = "单周";
if (SinOrDou == 2)
GVStuClass.Rows[i].Cells[6].Text = "双周";
if (SinOrDou == 0)
GVStuClass.Rows[i].Cells[6].Text = "全周";
GVStuClass.Rows[i].Cells[2].Text = teacherName;
GVStuClass.Rows[i].Cells[1].Text = courseName;
GVStuClass.Rows[i].Cells[7].Text = departName;
}

}
...全文
164 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
renyiqiu 2011-06-10
  • 打赏
  • 举报
回复
帮LZ顶!等待高手
syb1045 2011-06-09
  • 打赏
  • 举报
回复

加一个 ondatabound="GridView1_DataBound"
protected void GridView1_DataBound(object sender, EventArgs e)
{
处理的代码写在这里面
}

试试
子夜__ 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 s_zxing 的回复:]

引用 5 楼 ssp2009 的回复:

其实你完全可以在数据库里处理这些事情
SQL code
select (case SinOrDou when 1 then '单周' when 2 then '双周' else '全周') as SinOrDou from tb

这个处理这列是没问题的,其他的呢,怎么办?
怎么说,我用的是三层结构,这样的话,那BLL里的函数得多得写死…………
[/Quote]
其实3层 也可以在数据库里完成逻辑的。
ivenlove 2011-06-09
  • 打赏
  • 举报
回复
用RowBindDate事件处理。
S_zxing 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jejexu 的回复:]

你可以这样的.前台用模板C# code
<dx:GridViewDataColumn Caption="授课教师" VisibleIndex="14" Width="100%" > <DataItemTemplate>
<%#GeteacherNamebyId(Eval("Te……
[/Quote]
这样用太麻烦啊,有没有更简单点的,一定要用gridviewdatacolumn这个东西啊?
S_zxing 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jejexu 的回复:]

你可以这样的.前台用模板C# code
<dx:GridViewDataColumn Caption="授课教师" VisibleIndex="14" Width="100%" > <DataItemTemplate>
<%#GeteacherNamebyId(Eval("Te……
[/Quote]
这个gridviewdatacolumn到底是怎么用的,没用过啊,有资料吗?谢谢……
快溜 2011-06-09
  • 打赏
  • 举报
回复
说实话,你这样写的真不应该啊,GetTeacherByTeacherID()光是这些方法都没必要,完全可以在数据库里联合查询出来所有的Name,不明白你的函数是什么意思。。
S_zxing 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ssp2009 的回复:]

其实你完全可以在数据库里处理这些事情
SQL code
select (case SinOrDou when 1 then '单周' when 2 then '双周' else '全周') as SinOrDou from tb
[/Quote]
这个处理这列是没问题的,其他的呢,怎么办?
怎么说,我用的是三层结构,这样的话,那BLL里的函数得多得写死……
S_zxing 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 syb1045 的回复:]

引用楼主 s_zxing 的回复:
用GridView在后台绑定数据库,并将相应行中相应列数据处理一下,不过就是显示不出来处理后的数据,诡异啊,以前是可以实现这样的,唉,我不知道是哪里出问题了,请帮忙看看,不胜感激!下面是代码……
前台代码:
其中要处理的是,teacherID,courseID,Point,SinOrDou

C# code

<asp:GridView ID="……
[/Quote]
自己调试过,调试信息里面的东西已经是我想要的,但是就是在GridView里面显示不出来,是不是有什么属性没设啊
快溜 2011-06-09
  • 打赏
  • 举报
回复
其实你完全可以在数据库里处理这些事情
select (case SinOrDou when 1 then '单周' when 2 then '双周' else '全周') as SinOrDou from tb
syb1045 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 s_zxing 的回复:]
用GridView在后台绑定数据库,并将相应行中相应列数据处理一下,不过就是显示不出来处理后的数据,诡异啊,以前是可以实现这样的,唉,我不知道是哪里出问题了,请帮忙看看,不胜感激!下面是代码……
前台代码:
其中要处理的是,teacherID,courseID,Point,SinOrDou

C# code

<asp:GridView ID="GVStuClass" runat="s……
[/Quote]
LS的这个可以, 楼主的代码, 也是可以处理列的, 仔细调试下代码,看看是否进入循环,在循环里面是怎么执行的
S_zxing 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 kevin87923 的回复:]

改成TemplateField看看
[/Quote]
改成这个是没用的。。。
我先试试2楼的办法,不过这样我就得在后台写4个这样的函数了,唉……
kevin87923 2011-06-09
  • 打赏
  • 举报
回复
改成TemplateField看看
jeje 2011-06-09
  • 打赏
  • 举报
回复
你可以这样的.前台用模板
<dx:GridViewDataColumn Caption="授课教师" VisibleIndex="14" Width="100%" >                            <DataItemTemplate>
<%#GeteacherNamebyId(Eval("TeacherID"))%>
</DataItemTemplate>
<EditFormSettings Visible="False" />
</dx:GridViewDataColumn>

后台写一个根据
public string GeteacherNamebyId(object teacherID)
{
int id=ConvertToInt32(teacherID)
string teacherName = teacherObject.GetTeacherByTeacherID(id);
return teacherName ;
}

62,046

社区成员

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

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

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

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