vishal patil

vishal patil

  • NA
  • 7
  • 6.1k

How to upload image in datagridtemplatecolumn

Jun 29 2014 3:46 AM
How to upload image in datagridtemplatecolumn image show in runtime in datagridtemplatecoulmn wpf c#?

Code:

<UserControl x:Class="design1.newuser"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Loaded="UserControl_Loaded">
    <Grid Height="610" Width="810">
        <DataGrid  ItemsSource="{Binding customer}"    AutoGenerateColumns="False" Height="405" HorizontalAlignment="Left" Margin="12,95,0,0" Name="dgvreg" VerticalAlignment="Top" Width="786"  
     CanUserAddRows="True" 
           CanUserDeleteRows="True"
           CanUserSortColumns="True"
           CanUserReorderColumns="True">
            <DataGrid.Style>
                <Style>
                    
                    <Setter Property="Button.FontSize" Value="12" />
                    <Setter Property="Button.FontWeight" Value="Bold" />
                </Style>
            </DataGrid.Style>
            <DataGrid.Columns >
                <DataGridTemplateColumn  >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button  Content="upload" Click="btnUpload_Click" ></Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="upload your picture"   >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding upload_your_picture}" Name="image2" Height="40" Width="60"/> 
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="536,26,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
    </Grid>
</UserControl>

Code:
 private void btnUpload_Click(object sender, RoutedEventArgs e)
        {
            var id1 = (DataRowView)dgvreg.SelectedItem;          
            
            string upload = Convert.ToString(id1.Row["upload_your_picture"].ToString());
            
            ImageClass imgs = new ImageClass();
            Context DB = new Context();
            OpenFileDialog OpenFileObj = new OpenFileDialog();
            OpenFileObj.ShowDialog();  
           
            upload= OpenFileObj.FileName;
            ImageSource imgsource = new BitmapImage(new Uri(upload));
          
        }