Question

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!

Était-ce utile?

La solution

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

enter image description here

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

Autres conseils

Changed the text for the labels to

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

and commented out the bulk of the event.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top