c# - bind gridview dynamically with label and tooltip -


I want to force the gridview with datatable in the back code and I add a label in each cell in gridview and on a tooltip I want to display their values ​​label ... I can not find the tool tip .. (I want to display each seat of that stage) !!! And want to tooltip it, and my platform can change, so I want to dynamically)

Help me. My code is here

// on the grid view page It is bound to give me the index was out of bounds should be less than the non-negotiable and the size of the collection. Parameter Name: Index for

  Protected Zero GridView1_RowDataBound {Object Sender, GridViewRowEventArgs E} {{int i = 0; I & lt; table.Rows.Count; i ++} (Int j = 0; j and lt; table.color; 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 must add a line to each cell in the label . I would suggest to store information for label and tooltips in the Data Key archive, and add the label to the OnRowDataBound event.

Option 1:

Edit : Added markup to show how to add data keys

Define the data keys like the example below, replace LabelTextColumn and TooltipTextColumn with the text you want to use for text and tooltips Are there. Also, note that the OnRowDataBound event handler is set to:

  & lt; ASP: GridView id = "GridView1" runat = "Server" DataKeyNames = "LabelTextColumn, TooltipTextColumn" OnRowDataBound = "GridView1_RowDataBound". .. & gt; & Lt; Columns & gt; & Lt; ASP: TemplateField & gt; & Lt; ItemTemplate & gt; & Lt; Asp: Label ID = "Label1" runat = "server" / & gt; & Lt; / ItemTemplate & gt; & Lt; / ASP: TemplateField & gt; ... & lt; / Column & gt; & Lt; / ASP: GridView & gt;   

edit : The correct error for getting data key using RowIndex

Since you are in RowDataBoundEvent, you use No need to loop The RowDataBound event is a data loop in the form of a loop, and for this reason you have used the current line with e.row

 The code is called> Protected Zero GridView1_RowDataBound (Object Sender, GridViewRowEventArgs E) {// We do not need it, because the label is in line // Label LBL = already in the new label (); Get labeled with // row label lbl = (label) e.row.FindControl ("Label1"); - Markup lbl to text and tooltip text. Using the datatack specified in text = grd.DataKeys [e.row.RowIndex] ["LabelTextColumn"]. ToString (); Lbl.ToolTip = grd.DataKeys [e.Row.RowIndex] ["TooltipTextColumn"]. ToString (); // We do not need it anymore, because the label has already been added to the line //e.Row.Cells[0]. Control. Add (LLL); }   

Option 2: Using the inline avalue function to set text and tooltip text

  & lt ; Asp: GridView id = "GridView1" runat = "server" ... & gt; & Lt; Columns & gt; & Lt; ASP: TemplateField & gt; & Lt; ItemTemplate & gt; & Lt; Asp: Label ID = "Label1" runat = "server" text = '& lt;% # Eval ("LabelTextColumn")' tooltip = '& lt;% # Eval ("TooltipTextColumn")% & gt; / & Gt; & Lt; / ItemTemplate & gt; & Lt; / ASP: TemplateField & gt; ... & lt; / Column & gt; & Lt; / ASP: GridView & gt;    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -