dot Net Grid View Control Problems

Some times we are facing problems with grid view control that we can't retrieve value from controls which are placed in grid view template fields in VS .Net 3.5 & for this problem we have to add controls in template fields at runtime. For this we have to code on GridView's RowDataBound event.

For Example :
[VB]

Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
Try
Dim txt as new TextBox
e.Row.Cells(0).Controls.Add(txt)
Catch ex As Exception
End Try
End Sub

[C#]
protected
void gv_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)

    {
    try
    {
    TextBox txt = new TextBox();
    e.Row.Cells(0).Controls.Add(txt);
    }
    catch (Exception ex)
    { }

    }


Note :: this cell(0) is templatefield.


And for retreiving value we can code like this :

first we have to typecast this template field control in its type os control like i m typcasting its textbox in a textbox.

[VB]

Try
Dim txt as new TextBox
txt = gv.Row.Cells(0).Controls(0)

txt.Text 'you can use it anywhere

Catch ex As Exception
End Try

[C#]

    try
    {
    TextBox txt = new TextBox();

    txt = gv.Row.Cells(0).Controls(0);

    txt.Text //you can use it anywhere

    } catch (Exception ex)

    { }

Monday, April 13, 2009

0 Comments: