Link to home
Start Free TrialLog in
Avatar of nickbailey111
nickbailey111

asked on

asp.net/C# - programatically read a gridview?

I have a gridview that displays results from a table "A". Id like to say in the code behind, "if there's a value in column A, display a text box called "foo", if not, display a drop down list "foobar". For some reason, I can't think of how to "read" values on the C# side...can someone help? The way I'm trying to do it currently is to add a template field to the GV, insert a text box and a DDL control, both with visible=false. Then if I can figure out "reading" the value, I'll set it to "visible=true". I'm sure there is a better method though.
Avatar of crazyman
crazyman
Flag of United Kingdom of Great Britain and Northern Ireland image

You can indeed do what you are thinking in the ItemDataBound event you can examine the details of the row and manipulate the controls contained in the template.
Avatar of nickbailey111
nickbailey111

ASKER

thanks - could you help me figure it out?
Got no VS with me right now but you need to wire up the grid to the ItemDataBound event, something like:


protected void Grid1_ItemDataBound(object sender,GridItemEventArgs e)
{
        string test = e.Item.Cells[0].Text;
        TextBox tb = e.Item.FindControl("txtTextBox") as TextBox;
        DropDownList dd = e.Item.FindControl("ddlMyList") as DropDownList;
        tb.Visible = test == "1";
        dd.Visible = !tb.Visible;
}

Open in new window

i guess that's my problem - no such args as griditemeventargs

I guessed the name, got no IDE with me.

You can get the designer to make the correct signature for you...


 protected void Grid1_ItemDataBound(object sender,DataGridItemEventArgs e)
{
if((e.Item.ItemType == ListItemType.Item) || 
             (e.Item.ItemType == ListItemType.AlternatingItem))
             {
                 string test = e.Item.Cells[0].Text;
                 TextBox tb = e.Item.FindControl("txtTextBox") as TextBox;
                 DropDownList dd = e.Item.FindControl("ddlMyList") as DropDownList;
                 tb.Visible = test == "1";
                 dd.Visible = !tb.Visible;
 
             }
        }
 

Open in new window

i see my confusion for the moment - I am using GridView event args, not DataGrid args....experimenting with GridViewRowEventArgs...will post back.
ag, what am I doing wrong? I can't seem to find the control!

<asp:GridView ID="GV_Vars" runat="server" DataSourceID="ODS_Variables" 
                onrowcreated="OnRowCreated">
                <Columns>
                    <asp:BoundField DataField="var1_name" HeaderText="FOO" 
                        SortExpression="var1_name" />
                    <asp:TemplateField HeaderText = "INPUT" runat="server">
                        <ItemTemplate>
                            <asp:TextBox ID="TB_INPUT" runat="server" Visible="false"></asp:TextBox>
                            <asp:DropDownList ID="DDL_INPUT" runat="server" Visible="false">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
 
Code Behind:
        protected void OnRowCreated(object sender,GridViewRowEventArgs e)
        {
            string foo = e.Row.Cells[1].Text;
            if (foo != "")
            {
                TextBox tb = e.Row.Cells[3].FindControl("TB_INPUT") as TextBox;
                tb.Visible = true;
            }
        }

Open in new window

should be Cells[2], not Cells[3], I had removed a column in between for examples' sake.
ASKER CERTIFIED SOLUTION
Avatar of crazyman
crazyman
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial