为多个dropdownlist赋值

feifei_luntan 2010-09-25 01:20:40
数据库表数据:
id(varchar) name(varchar) banzhong(int) date(smalldatetime)
admin a 3 2010-09-01 00:00:00
admin a 1 2010-09-02 00:00:00
admin a 1 2010-09-03 00:00:00
admin a 4 2010-09-04 00:00:00
admin a 4 2010-09-05 00:00:00
希望在gridview 控件上得到如下效果:

其中用到了dropdownlist,主要是不知道如何向gridview 模板列中的多个dropdownlist赋值,其中列banzhong=1(早班),2(午班),3(晚班),4(公休)
...全文
353 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
feifei_luntan 2010-09-26
  • 打赏
  • 举报
回复
谢了,结贴
wangshanmin 2010-09-26
  • 打赏
  • 举报
回复
<asp:DataGrid ID="DG1" runat="server" Width="2560px" AutoGenerateColumns="False">
<Columns>

<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="0"> </asp:ListItem>
<asp:ListItem Value="1">早班</asp:ListItem>
<asp:ListItem Value="2" >中班</asp:ListItem>
<asp:ListItem Value="3">晚班</asp:ListItem>
<asp:ListItem Value="4">公休</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle width="80px" />
</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Value="0"> </asp:ListItem>
<asp:ListItem Value="1" >早班</asp:ListItem>
<asp:ListItem Value="2" >中班</asp:ListItem>
<asp:ListItem Value="3">晚班</asp:ListItem>
<asp:ListItem Value="4">公休</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle width="80px" />
</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server">
<asp:ListItem Value="0"> </asp:ListItem>
<asp:ListItem Value="1" Selected="True">早班</asp:ListItem>
<asp:ListItem Value="2" >中班</asp:ListItem>
<asp:ListItem Value="3">晚班</asp:ListItem>
<asp:ListItem Value="4">公休</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle Width="80px" />
</asp:TemplateField>
</Columns>
</asp:DataGrid>



DataTable dt = new DataTable();
dt = xxxx.ssss(a, b, ref dt);
int j = dt.行数。
for (int i = 0; i < j; i++)
{
DataRow dr = dt.NewRow();
DropDownList ddl = DataGrid1.Items[i].Cells[2].FindControl("DropDownList1") as DropDownList;
ddl.Value=dr["banchong1"];
DropDownList ddl = DataGrid1.Items[i].Cells[3].FindControl("DropDownList2") as DropDownList;
ddl.Value=dr["banchong2"];
DropDownList ddl = DataGrid1.Items[i].Cells[4].FindControl("DropDownList3") as DropDownList;
ddl.Value=dr["banchong3"];
dt.Rows.Add(dr);
}

这样不行吗,我是新手,不对勿怪.
feifei_luntan 2010-09-26
  • 打赏
  • 举报
回复
急求,请高手给个建议。。。
feifei_luntan 2010-09-25
  • 打赏
  • 举报
回复
诚心感谢,可能我的思路不正确把,请给下建议,大致要求是这样的:
一个人员排班表如下:

希望保存完排班后,再次进入此页时,《1》把排班情况在gridview的dropdownlist控件上展示出来

《2》通过对dropdownlist控件中数据的修改,完成在此页对人员排班表的修改
请给下建议,谢了

wuyq11 2010-09-25
  • 打赏
  • 举报
回复
<ItemTemplate>
<asp:DropDownList ID="ddlSort" runat="server" Width="98%">
</asp:DropDownList>
<asp:HiddenField ID="Hd_Sort" runat="server" Value='<%# Eval("BalSort")%>' />
</ItemTemplate>
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{ if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
((DropDownList)e.Row.FindControl("ddlSort")).SelectedValue = ((HiddenField)e.Row.FindControl("Hd_Sort")).Value;
}
}
}

protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
string strSort = ((DropDownList)gv.Rows[e.NewEditIndex].FindControl("ddlSort")).SelectedValue.Trim();


gv.EditIndex = -1;
BindData();
}
feifei_luntan 2010-09-25
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 chazikai24 的回复:]
把数据库的相关数据取出来,放到一个datatable,这个你实现了已经;
然后用上面的for循环,如果datatable第i行第j列的值为1,则datagridview1.rows[i].cells[j].value = "早班";如果是2,依次类推。
根据datatable的情况给datagridview赋值。
应该非常简单了吧?
这个前台网页我没做过
[/Quote]
诚心感谢,可能我的思路不正确把,请给下建议,大致要求是这样的:
一个人员排班表如下:

希望保存完排班后,再次进入此页时,《1》把排班情况在gridview的dropdownlist控件上展示出来

《2》通过对dropdownlist控件中数据的修改,完成在此页对人员排班表的修改
请给下建议,谢了


沝林 2010-09-25
  • 打赏
  • 举报
回复
你可以把数据源固定到31列,显示的时候如果不足31列就隐藏掉实际不存在的列,这样数据源就是固定的了,绑定的时候可以这样做:

<asp:ListItem Value="1" Selected='绑定列值 == 1'>早班</asp:ListItem>
<asp:ListItem Value="2" Selected='绑定列值 == 2'>中班</asp:ListItem>
<asp:ListItem Value="3" Selected='绑定列值 == 3'>晚班</asp:ListItem>
<asp:ListItem Value="4" Selected='绑定列值 == 4'>公休</asp:ListItem>
chazikai24 2010-09-25
  • 打赏
  • 举报
回复
把数据库的相关数据取出来,放到一个datatable,这个你实现了已经;
然后用上面的for循环,如果datatable第i行第j列的值为1,则datagridview1.rows[i].cells[j].value = "早班";如果是2,依次类推。
根据datatable的情况给datagridview赋值。
应该非常简单了吧?
这个前台网页我没做过
feifei_luntan 2010-09-25
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 duanzilin 的回复:]
绑定到数据源就可以自动绑定下拉列表的值,干嘛要循环赋值,把模板列中dropdownlist的SelectedValue属性绑定到数据源的列名,应该就可以了
[/Quote]
可是数据源的列名不确定啊,例如每个月的天数都不同啊
沝林 2010-09-25
  • 打赏
  • 举报
回复
绑定到数据源就可以自动绑定下拉列表的值,干嘛要循环赋值,把模板列中dropdownlist的SelectedValue属性绑定到数据源的列名,应该就可以了
feifei_luntan 2010-09-25
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 chazikai24 的回复:]
for(int i = 0;i<datagridview1.rows.count;i++)
{
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
if (dataGridView1.Rows[i].Cells[j].ToString() == "1")
{
dataGridView1.Rows[i].Cell……
[/Quote]
谢谢12楼的诚心帮忙,可是我希望gridview 的模板列中添加dropdownlist控件,然后根据数据库中的班种取值。
前台代码如下:

<asp:GridView ID="gv" runat="server" Width="2560px" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="u_id">
<HeaderStyle Width="80px" />
</asp:BoundField>
<asp:BoundField DataField="u_name" >
<HeaderStyle Width="80px"/>
</asp:BoundField>

<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList" runat="server">
<asp:ListItem Value="1" Selected="True">早班</asp:ListItem>
<asp:ListItem Value="2" >中班</asp:ListItem>
<asp:ListItem Value="3">晚班</asp:ListItem>
<asp:ListItem Value="4">公休</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle Width="80px" />
</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList" runat="server">
<asp:ListItem Value="1" Selected="True">早班</asp:ListItem>
<asp:ListItem Value="2" >中班</asp:ListItem>
<asp:ListItem Value="3">晚班</asp:ListItem>
<asp:ListItem Value="4">公休</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle Width="80px" />
</asp:TemplateField>
!---一共31个模板列

</Columns>
</asp:GridView>

数据源为datatable;
不用dropdownlist 的话,在gridview 中的效果是:

希望用dropdownlist控件,用控件展示出班种效果来
chazikai24 2010-09-25
  • 打赏
  • 举报
回复
for(int i = 0;i<datagridview1.rows.count;i++)
{
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
if (dataGridView1.Rows[i].Cells[j].ToString() == "1")
{
dataGridView1.Rows[i].Cells[j].Value = "早班";
}
else if (dataGridView1.Rows[i].Cells[j].ToString() == "2")
{
dataGridView1.Rows[i].Cells[j].Value = "中班";
}
else if (dataGridView1.Rows[i].Cells[j].ToString() == "3")
{
dataGridView1.Rows[i].Cells[j].Value = "晚班";
}
else if (dataGridView1.Rows[i].Cells[j].ToString() == "4")
{
dataGridView1.Rows[i].Cells[j].Value = "公休";
}
}
}
请自己测试,做一定的修改。我没有调试
feifei_luntan 2010-09-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 feifei_luntan 的回复:]
我可以得到如下效果,可是添加上dropdownlist就不会了,请高手指教
[/Quote]
各位大侠,我希望把gridview 中的数字,有dropdownlist展示出来。。。
chazikai24 2010-09-25
  • 打赏
  • 举报
回复
同志,for循环会吗?循环行与列,用swich或者4个判断来确定combobox的值
feifei_luntan 2010-09-25
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 yumen3501 的回复:]
(GridView.Rows[i].Cells[i].FindControl("DropDownList") as DropDownList).DataSource=‘你数据源’
[/Quote]
不太懂?
Assassin_ 2010-09-25
  • 打赏
  • 举报
回复
(GridView.Rows[i].Cells[i].FindControl("DropDownList") as DropDownList).DataSource=‘你数据源’
feifei_luntan 2010-09-25
  • 打赏
  • 举报
回复

我可以得到如下效果,可是添加上dropdownlist就不会了,请高手指教
tfbvip 2010-09-25
  • 打赏
  • 举报
回复
请高手进。。
chazikai24 2010-09-25
  • 打赏
  • 举报
回复
这个不是循环吗?,循环取datagridview的第一行的每一列,如果是1,则为早班,2为。。。。。
只是一个简单的循环而已
feifei_luntan 2010-09-25
  • 打赏
  • 举报
回复
请高手进。。
加载更多回复(3)

111,129

社区成员

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

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

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