Need help figuring out a way to input data into an asp:DataGrid

人盡茶涼 提交于 2019-12-11 20:32:22

问题


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.

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

Here is the event in question

 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?

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?

All help, advice, or critisims are appreciated!


回答1:


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();
    }
}



回答2:


Changed the text for the labels to

Text='<%# Bind("BrokerMPID") %>'

and commented out the bulk of the event.



来源:https://stackoverflow.com/questions/19414429/need-help-figuring-out-a-way-to-input-data-into-an-aspdatagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!