需要帮忙想办法将数据输入到一个asp:DataGrid中 [英] Need help figuring out a way to input data into an asp:DataGrid

查看:160
本文介绍了需要帮忙想办法将数据输入到一个asp:DataGrid中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp:DataGrid中我需要填充,和原来的开发人员创建一个奇怪的包装用很少的灵活性,所以我决定将其切换到数据表来代替。唯一的问题是,它使用来填充我的网格不喜欢我的DataTable事件。它口口声声说,它不能从一个DataTable转换为DataRowView的,这是有道理的。

I have an asp:DataGrid I need to populate, and the original developer created a weird wrapper with very little flexibility, so I have decided to switch it over to a DataTable instead. The only issue is the event that it uses to populate my grid does not like my datatables. It keeps saying that it cannot convert from a DataTable to a DataRowView, which makes sense.

我需要帮助figureing从我的DataTable到DataGrid中传递数据的好办法。

I need help figureing a good way to pass the data from my DataTable into the dataGrid.

下面是有问题的事件。

 protected override void ReflectItem(DataGridItemEventArgs e)
  {
     if (e.Item.DataItem == null) return;

     DataTable currentItem = (DataTable)e.Item.DataItem;
     if (currentItem == null) return;

     var labelBrokerMPID = (Label)e.Item.FindControl("labelBrokerMPID");
     var labelBrokerName = (Label)e.Item.FindControl("labelBrokerName");
     var labelClearingBrokerDTC = (Label)e.Item.FindControl("labelClearingBrokerDTC");
     //var labelClearingBrokerName = (Label)e.Item.FindControl("labelClearingBrokerName");
     var linkButtonViewBroker = (LinkButton)e.Item.FindControl("linkButtonViewBroker");
     var linkButtonDeleteBroker = (LinkButton)e.Item.FindControl("linkButtonDeleteBroker");

     labelBrokerMPID.Text = currentItem.Rows[0]["BrokerMPID"].ToString();
     labelBrokerName.Text = currentItem.Rows[0]["BrokerName"].ToString();
     labelClearingBrokerDTC.Text = currentItem.Rows[0]["ClearingBrokerDTC"].ToString();
     linkButtonViewBroker.CommandArgument = currentItem.Rows[0]["RelationshipId"].ToString();
  }

显然,数据表不靠谱。我找到网格,因为他们使用的标签,而不是绑定列有点沮丧。难道是我更容易与绑定列格式化数据网格,而不是标签?还是有我的数据传递一个更简单的方法?

Obviously the Datatables don't fly. I find the grid a bit frustrating since they used labels as opposed to bound columns. Would it be easier for me to reformat the data grid with bound columns as opposed to labels? Or is there an easier way to pass in my data?

截至目前,我在我的数据表存储在一个会话变量,并将其绑定到DataGrid。这就是这个错误时,边棱。有没有一种简单的方法来我的数据从一个DataTable转换为DataRowView的?

As of now I store my datatable in a session variable and bind it to the DataGrid. That is when this error arrises. Is there an easy way to convert my data from a datatable to a dataRowView?

所有的帮助,建议,或critisims是AP preciated!

All help, advice, or critisims are appreciated!

推荐答案

您几乎没有。你只需要投 e.Item.DataItem DataRowView的

You are almost there. You just need to cast e.Item.DataItem to DataRowView.

<asp:DataGrid ID="DataGrid1" runat="server" 
    OnItemDataBound="DataGrid1_ItemDataBound" 
    AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateColumn HeaderText="Broker">
            <ItemTemplate>
                <asp:Label ID="labelBrokerMPID" runat="server" /> - 
                <asp:Label ID="labelBrokerName" runat="server" />
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid>

public DataTable MyDataSource
{
    get
    {
        var dt = new DataTable();
        dt.Columns.Add(new DataColumn("BrokerMPID", typeof (Int32)));
        dt.Columns.Add(new DataColumn("BrokerName", typeof (string)));

        for (int i = 0; i < 5; i++)
        {
            var dr = dt.NewRow();
            dr[0] = i;
            dr[1] = "Name " + i;
            dt.Rows.Add(dr);
        }
        return dt;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataGrid1.DataSource = MyDataSource;
        DataGrid1.DataBind();
    }
}

protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item ||
        e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var dr = e.Item.DataItem as DataRowView;

        var labelBrokerMPID = e.Item.FindControl("labelBrokerMPID") as Label;
        labelBrokerMPID.Text = dr["BrokerMPID"].ToString();

        var labelBrokerName = e.Item.FindControl("labelBrokerName") as Label;
        labelBrokerName.Text = dr["BrokerName"].ToString();
    }
}

这篇关于需要帮忙想办法将数据输入到一个asp:DataGrid中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆