开发者

bind gridview dynamically with label and tooltip

开发者 https://www.devze.com 2023-04-02 05:56 出处:网络
i want to bind gridview with datatable in code behind and i want to add label in each cell in gridview and display a tooltip on label of their values... i am not getting tool tip..

i want to bind gridview with datatable in code behind and i want to add label in each cell in gridview and display a tooltip on label of their values... i am not getting tool tip.. (i want to display an each seat of thatre stage) !!! and want to tooltip of it,and my stage may change,so i want dynamically)

help me.. my code is here

//Grid view is bind on page load it gives me Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

开发者_高级运维
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{


            for (int i = 0; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < table.Columns.Count; j++)
                {


                    Label lbl = new Label();
                    lbl.Text = GridView1.DataKeys[e.Row.RowIndex]["LabelText"].ToString();
                    lbl.ToolTip = GridView1.DataKeys[e.Row.RowIndex]["TooltipText"].ToString();
                    e.Row.Cells[0].Controls.Add(lbl);

                }

            }

}


You need to add the Label to a cell in each row of the GridView. I would suggest storing the information for the Label and the tooltip in the data key collection, and adding the label in the OnRowDataBound event.

Option 1:

EDIT: Added markup to show how to add data keys

Define the data keys like in the example below. Replace LabelTextColumn and TooltipTextColumn with the actual you want to use for the text and the tooltip. Also, notice how the OnRowDataBound event handler is set here:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="LabelTextColumn, TooltipTextColumn" OnRowDataBound="GridView1_RowDataBound" ...>
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        ...
    </Columns>
</asp:GridView>

EDIT: Corrected error using RowIndex to get data keys

Since you're in the RowDataBoundEvent, you don't need to use a loop. The RowDataBound event is called from within a loop as each row is databound, which is why you have access the current row with e.Row

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //we don't need this anymore, because the label is already in the row
    //Label lbl = new Label(); 

    //get the label from the row
    Label lbl = (Label)e.Row.FindControl("Label1");

    --set the text and tooltip text using the datakeys specified in the markup
    lbl.Text = grd.DataKeys[e.Row.RowIndex]["LabelTextColumn"].ToString();
    lbl.ToolTip = grd.DataKeys[e.Row.RowIndex]["TooltipTextColumn"].ToString();

    //we don't need this anymore either, because the label is already added to the row
    //e.Row.Cells[0].Controls.Add(lbl);
}

Option 2: Using inline Eval() function to set the text and tooltip text

<asp:GridView ID="GridView1" runat="server"  ...>
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%#Eval("LabelTextColumn")' Tooltip='<%#Eval("TooltipTextColumn")%>' />
            </ItemTemplate>
        </asp:TemplateField>
        ...
    </Columns>
</asp:GridView>


When you databind, it destroys the current control collection and populates with the provided data source.

For your specific application, you would also need to attach to the GridView.RowCreated event and then insert the tooltip you need.


Implement this in a handler for the Gridview.RowDataBound Event.

0

精彩评论

暂无评论...
验证码 换一张
取 消