IList bu_transList = new ArrayList();
SqlCommand cmd = GenerateCmd("sp_Getbu_transKw");
using (SqlDataReader rdr = ExeSql4rdr(cmd))
{
while (rdr.Read())
{
Bu_TranModel bu_tran= new Bu_TranModel();
bu_tran.bu_no = rdr["bu_no"].ToString();
                  bu_tran.name = rdr["name"].ToString();
// bu_tran.date = Convert.ToDateTime(rdr["date"]);
// bu_tran.hdate = Convert.ToDateTime(rdr["hdate"]);
bu_transList.Add(bu_tran);
}
}
比如这样得到了bu_tran类的集合bu_transList,要怎么跟datagrid绑定呢,每列一个字段,还有编辑的时候变成输入框或下拉框。
对了,还有注释的那两行如果数据库里值为空这样会出错,如何解决?
谢谢大家了

解决方案 »

  1.   

    ,还有注释的那两行如果数据库里值为空这样会出错,如何解决?
    答案::
    你把sql语言写成 select isNull('列',"") as 列明! 
    比如这样得到了bu_tran类的集合bu_transList,要怎么跟datagrid绑定呢,每列一个字段,还有编辑的时候变成输入框或下拉框。
    aspx::
    <asp:datagrid id="projectinfo" runat="server" Width="80%" BorderColor="#444444" AutoGenerateColumns="False"
    CellPadding="1" PageSize="18">
    <SelectedItemStyle Wrap="False"></SelectedItemStyle>
    <EditItemStyle Wrap="False"></EditItemStyle>
    <AlternatingItemStyle Wrap="False"></AlternatingItemStyle>
    <ItemStyle Font-Size="13px" Wrap="False" Height="17px"></ItemStyle>
    <HeaderStyle Font-Size="14px" Wrap="False" Height="20px" ForeColor="White" BackColor="#990000"></HeaderStyle>
    <FooterStyle Wrap="False"></FooterStyle>
    <Columns>
    <asp:TemplateColumn HeaderText="点击选中">
    <ItemStyle Width="60px"></ItemStyle>
    <ItemTemplate>
    <asp:CheckBox ID="mycheck" Runat="server"></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="zymc" HeaderText="名称"></asp:BoundColumn>
    <asp:TemplateColumn HeaderText="人员">
    <ItemTemplate>
    <asp:DropDownList ID="mydropdownlist" DataSource='<%# FristCensorResult(DataBinder.Eval(Container.DataItem,"zyid")) %>' Runat="server" >
    <asp:ListItem>fsd</asp:ListItem>
    <asp:ListItem>fsd</asp:ListItem>
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn Visible="False" DataField="zyid"></asp:BoundColumn>
    </Columns>
    <PagerStyle NextPageText="&gt;" PrevPageText="&lt;" Mode="NumericPages"></PagerStyle>
    </asp:datagrid>
    cs::namespace WebUI.fpzyry
    {
    /// <summary>
    /// assinryform 的摘要说明。
    /// </summary>
    public class assinryform : PageBase
    {
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.DataGrid projectinfo;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!IsPostBack)
    {
    BindData();
    }
    }
    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.projectinfo.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.projectinfo_ItemDataBound);
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    private void BindData()
    {
    SqlDataReader dr = (new FAssignPro()).GetListAllZY();
                                //获得数据源是三层架构的 projectinfo.DataSource= dr;
    projectinfo.DataBind();
    if(!dr.IsClosed)
    {
    dr.Close();
    dr = null;
    }
    } private void projectinfo_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
    {


    /*System.Data.SqlClient.SqlTransaction sdf= new System.Data.SqlClient.SqlTransaction();
    try
    { }
    catch(Exception e1)
    {
      
    sdf.Rollback();
    }
      */
        
                      
    }
    } //System.Data.SqlClient.SqlConnection   sfd= System.Data.SqlClient.SqlConnection();
    public ArrayList  FristCensorResult(object  popo)
    {

    return (new FAssignPro()).GetAssinZY(popo.ToString());
                                  //获得数据源是三层架构的
    } private void Button1_Click(object sender, System.EventArgs e)
    { }
        

    }
    }
      

  2.   

    roapzone(宗璞)兄:上面的例子是绑定datareader,我是想知道如何绑定集合select isNull('列',"")这句只是说如果是null就读出为""吧但是bu_tran.date = Convert.ToDateTime("");也是不行的啊望大家帮忙,谢了