locked
Updating gridview RRS feed

  • Question

  •   How to set the content of a hyperlink button (that is in the data template of gridview ) from  code behind  on a button

    click.I want to set the content on selected item of gridview.

    when i select an item of gridiew , a panel with a button will be enabled ,when i click the button some text should be appeared on the selected item of gridview.

    Can u pls help me on this issue.I am trying a lot on this issue.


    Thursday, April 25, 2013 6:56 AM

Answers

  • In your grid view row updating event add the following condition
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
     if (e.Row.RowState == DataControlRowState.Edit )
     {
    
    
        int key = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
    
        Label CompanyID = (Label)GridView1.Rows[e.RowIndex].FindControl("txtCompanyID");
    
        TextBox thisIssueDate = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtIssueDate"));
    
        TextBox NoticeIntentResponseDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNoticeIntentResponseDue");
    
        Response.Write(NoticeIntentResponseDue.Text + " " + thisIssueDate.Text);
        Response.End();
    
        TextBox DeadlineForQuestions = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDeadlineForQuestions");
    
        TextBox BidsDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBidsDue");
    
        TextBox ShortlistNotice = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtShortlistNotice");
    
        TextBox FinalSelection = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFinalSelection");
        }  
      }

    Update: the problem looks like that you have also bind your Edit Item template columns with the data from data table, and when you are getting the data in the code behind you are not getting the updated data which the user updates in edit mode and u still getting the old data. If you remove the Binding from the Edit Item Template feilds then your code will work.

    • Marked as answer by Jesse Jiang Tuesday, May 7, 2013 6:24 AM
    Monday, April 29, 2013 4:40 PM

All replies

  • How to set the text for a textbox of Gridview from code behind.Can u please help me on this.

    Thanks,

    syamala.

    Sunday, April 21, 2013 12:20 PM
  • There is not a good way to access the controls in a gridview from the code behind.  The best method is to bind the textbox's text  to a property in a class.  When you want to update the text change the value of the property
    Sunday, April 21, 2013 12:38 PM
  • Here's a working example to get textbox text.

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <GridView x:Name="myGrdiView" SelectionMode="None" ItemClick="myGrdiView_ItemClick" IsItemClickEnabled="True">
                <GridViewItem>
                    <TextBox x:Name="myTextBox" Text="Some Text" IsHitTestVisible="False"/>
                </GridViewItem>
            </GridView>
        </Grid>
    private async void myGrdiView_ItemClick(object sender, ItemClickEventArgs e)
            {
                var textBox = e.ClickedItem as TextBox;
                if (textBox != null)
                {
                    var dlg = new MessageDialog(textBox.Text);
                    await dlg.ShowAsync();
                }
            }

    Then do what ever you want with the text.

    Hope it helps.

    Sunday, April 21, 2013 3:21 PM
  • Here's a revised version with button click to get text. Remember the GridViewItem must be selected(swiped) in order for this to work.

     <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <GridView x:Name="myGrdiView" SelectionMode="Single" ItemClick="myGrdiView_ItemClick" IsItemClickEnabled="True">
                <GridViewItem>
                    <TextBox x:Name="myTextBox" Text="Some Text" IsHitTestVisible="False"/>
                </GridViewItem>
            </GridView>
            <Button x:Name="myButton" Content="Change Textbox Text" Click="myButton_Click"/>
        </Grid>

    Code Behind:

    private async void myGrdiView_ItemClick(object sender, ItemClickEventArgs e)
            {
                var textBox = e.ClickedItem as TextBox;
                if (textBox != null)
                {
                    var dlg = new MessageDialog(textBox.Text);
                    await dlg.ShowAsync();
                }
            }
            private async void myButton_Click(object sender, RoutedEventArgs e)
            {
                var gridViewItem = myGrdiView.SelectedItem as GridViewItem;
                if (gridViewItem != null)
                {
                    var textBox = gridViewItem.Content as TextBox;
                    if (textBox != null)
                    {
                        var dlg = new MessageDialog("this Button was Clicked and the TextBoxText is     :    "+textBox.Text);
                        await dlg.ShowAsync();
                    }
                }
            }

    Sunday, April 21, 2013 3:40 PM
  • I want to set the text not getting the text.

    Monday, April 22, 2013 6:37 AM

  • string myText = "hello world";
    
    private void myButton_Click(object sender, RoutedEventArgs e)
            {
                var gridViewItem = myGrdiView.SelectedItem as GridViewItem;
                if (gridViewItem != null)
                {
                    var textBox = gridViewItem.Content as TextBox;
                    if (textBox != null)
                    {
                        textBox.Text = myText;
                    }
                }
            }




    • Edited by MAMBO65 Monday, April 22, 2013 9:04 AM
    Monday, April 22, 2013 8:20 AM
  •    Is this the same way  to access the textbox in a datatemplate  of a gridview.

       Iam getting gridviewitem as null

    Monday, April 22, 2013 12:39 PM
  • Not the same way.

    Have a look here:

    http://stackoverflow.com/questions/15189715/how-to-access-a-control-inside-the-data-template-in-c-sharp-metro-ui-in-the-code

    http://stackoverflow.com/questions/15578688/access-datatemplate-element-in-a-listbox

    Monday, April 22, 2013 12:50 PM
  •       

       This is my Gridview:

     <GridView x:Name="list1" ItemsSource="{Binding}" Margin="0,100,0,0" 

                                       VerticalAlignment="Bottom" Padding="100,0,120,50" SelectionChanged="selection" ItemClick="item_click"  IsItemClickEnabled="True" Visibility="Collapsed">
                <GridView.ItemTemplate>

                    <DataTemplate >
                        <Grid HorizontalAlignment="Left" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
                            <StackPanel Orientation="Horizontal" Margin="10,10,0,0">
                                <Image Source="{Binding _Image}" Height="60" Width="60" VerticalAlignment="Center" Margin="0,0,10,0" />
                                <StackPanel Margin="0" Orientation="Vertical">
                                    <TextBlock TextWrapping="Wrap" 
                                               Foreground="{StaticResource ApplicationForegroundThemeBrush}" 
                                               Style="{StaticResource ItemTitleStyle}"
                                               Width="200"
                                               VerticalAlignment="Center"
                                               HorizontalAlignment="Left"
                                               Text="{Binding _Name}" />
                                    <GridViewItem>
                                
                                    <HyperlinkButton x:Name="hb" FontFamily="Arial"  FontStyle="Italic" FontSize="16"  Foreground="Blue" Content="{Binding _call}" Click="Info_click" />
                                    </GridViewItem>
                                </StackPanel>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>

                </GridView.ItemTemplate>
                <ScrollViewer HorizontalScrollBarVisibility="Visible"></ScrollViewer>

           </GridView>

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    When i select the item on the gridview(by right clicking),iteopens an appbar  below

                       

    <Page.BottomAppBar>
            <AppBar x:Name="BottomAppBar1" Opened="AppBar_Opened" IsSticky="True" Padding="10,0,10,0" AutomationProperties.Name="Bottom App Bar" Background="DarkOrange">

                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50*"/>
                        <ColumnDefinition Width="50*"/>
                    </Grid.ColumnDefinitions>
                    <StackPanel x:Name="LeftPanel" Orientation="Horizontal" Grid.Column="0" HorizontalAlignment="Left">
                        <Button x:Name="Edit" Style="{StaticResource EditAppBarButtonStyle}" Tag="Edit" Click="edit_click"/>
                        <!--<Button x:Name="Save" Style="{StaticResource SaveAppBarButtonStyle}" Tag="Save"/>-->
                        <Button x:Name="Remove" Style="{StaticResource RemoveAppBarButtonStyle}" Tag="Remove" Click="Remove_click"/>
                        <!--<Button x:Name="contactinfo" Style="{StaticResource ContactInfoAppBarButtonStyle}" Tag="ContactInfo" Click="Info_click"/>-->
                        <Button x:Name="getcall" Click="iccall" Content="Get a call" ></Button>

                        <Button x:Name="Addfriend" Style="{StaticResource AddFriendAppBarButtonStyle}" Tag="ContactInfo" Click="addfrnd_click"/>
                    </StackPanel>
                    <StackPanel x:Name="RightPanel" Orientation="Horizontal" Grid.Column="0" HorizontalAlignment="Right">

                        <Button x:Name="Add" Style="{StaticResource AddAppBarButtonStyle}" Tag="Add" Click="Add_click"/>
                    </StackPanel>

      </Grid>

            </AppBar>
        </Page.BottomAppBar>

    ------------------------------------------------------------------------------------------------------------------------

    The appbar contains getcall button which on click opens the below one

     <Grid x:Name="LayoutRoot" Visibility="Collapsed" Margin="0,50,28,0" Background="Gray" HorizontalAlignment="Right" VerticalAlignment="Top" Width="557">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="5*" />
                  
                    </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="5*"/>
                   
                   
                </Grid.RowDefinitions>
                <Image Source="image1.jpg" Height="80" Width="100" HorizontalAlignment="Left"></Image>
                <TextBlock Text="Incoming call......." FontSize="20"  Height="40" Margin="100,25,309,25"/>
               
                    <Button Width="96" Height="80" Margin="450,10,0,0" Click="accept">
                    <Image Source="call.png" Height="77" Width="96" Canvas.Left="185" Canvas.Top="213"/>
                </Button>

                <Button Width="96"  Margin="350,10,0,0" Height="80" Click="reject">
                    <Image Source="red_end_call.png" Height="109" Width="80" Canvas.Left="185" Canvas.Top="213"/>
                </Button>
            </Grid>

    ------------------------------------------------------------------------------------------------------------------------

    When i click on the accept button, it should display call in progress on the selected item of gridview.(i.e; string "call in progress") should be displayed as content of hyperlinkbutton named "hb" in the gridview.

    When i click on the reject button, it should display call rejected on the selected item of gridview.(i.e; string "call Rejected") should be displayed as content of hyperlinkbutton named "hb" in the gridview.

    This is my target.

    Sorry for the cumbersome explanation.Hope u can help me.

    Tuesday, April 23, 2013 6:43 AM
  • How can this can be done cab u quote me an example
    Thursday, April 25, 2013 6:38 AM
  • You'll just need to be able to reference the hyperlink button when it loads, so you can change it at runtime.  The way I do this is to capture a list of the hyperlinks using their loaded event. Hold on to that list, in order. When an item is selected, the index of that item will match the order in your list of hyperlinks. Get the hyperlink from your list and change it's content. That will update the gridview.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, April 25, 2013 2:36 PM
    Moderator
  • Also, please don't double post. I'm merging this with your other thread.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, April 25, 2013 2:38 PM
    Moderator
  •  can u give me sample code for this.
    Friday, April 26, 2013 6:07 AM
  • Please post a working sample.  Your code has a bunch of stuff in it that is not in a standard template.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, April 29, 2013 3:05 PM
    Moderator
  • In your grid view row updating event add the following condition
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
     if (e.Row.RowState == DataControlRowState.Edit )
     {
    
    
        int key = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
    
        Label CompanyID = (Label)GridView1.Rows[e.RowIndex].FindControl("txtCompanyID");
    
        TextBox thisIssueDate = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtIssueDate"));
    
        TextBox NoticeIntentResponseDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNoticeIntentResponseDue");
    
        Response.Write(NoticeIntentResponseDue.Text + " " + thisIssueDate.Text);
        Response.End();
    
        TextBox DeadlineForQuestions = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDeadlineForQuestions");
    
        TextBox BidsDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBidsDue");
    
        TextBox ShortlistNotice = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtShortlistNotice");
    
        TextBox FinalSelection = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFinalSelection");
        }  
      }

    Update: the problem looks like that you have also bind your Edit Item template columns with the data from data table, and when you are getting the data in the code behind you are not getting the updated data which the user updates in edit mode and u still getting the old data. If you remove the Binding from the Edit Item Template feilds then your code will work.

    • Marked as answer by Jesse Jiang Tuesday, May 7, 2013 6:24 AM
    Monday, April 29, 2013 4:40 PM