question

GauravBatra-3507 avatar image
0 Votes"
GauravBatra-3507 asked HuiLiu-MSFT commented

How to update wpf datagrid Selected value to sql server (Please modify my code and send back to me)

XAML coding :

<Window x:Class="NTS.frmViewITT"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
mc:Ignorable="d"
Title="frmITView" Height="650" Width="1000"
xmlns:staticData="clr-namespace:NTS"
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:my1="http://schemas.microsoft.com/netfx/2009/xaml/presentation">;


 <Window.Resources>
     <collections:ArrayList x:Key="arrList">
         <collections:DictionaryEntry Key="Open" Value="1"/>
         <collections:DictionaryEntry Key="Pending" Value="2"/>
         <collections:DictionaryEntry Key="Close" Value="3"/>
     </collections:ArrayList>
 </Window.Resources>
 <Grid >

     <StackPanel>
         <Label Content="View IT Tickets"></Label>
     </StackPanel>

     <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto">
         <DataGrid AlternatingRowBackground="#FFC4B0B0" Height="550" Width="950" AutoGenerateColumns="False" Name="grdEmp" CellEditEnding="GrdEmp_CellEditEnding" >
             <DataGrid.Columns>

                 <DataGridTextColumn Header="EmpID" Width="100" Binding="{Binding Empcode}"/>

                 <DataGridTextColumn Header="Employee Name" Width="100" Binding="{Binding EmployeeName}"/>

                 <DataGridTextColumn Header="Manager Name" Width="100" Binding="{Binding Manager}"/>

                 <DataGridTextColumn Header="Categories" Width="100" Binding="{Binding Categories}"/>
                 <DataGridTextColumn Header="Issue" Width="100" Binding="{Binding Issue}"/>
                    

                 <DataGridComboBoxColumn Header="Ticket Status" x:Name="gTicketStatus" Width="100" ItemsSource="{Binding TicketSt}" SelectedValuePath="TicketStatus" SelectedItemBinding="{Binding Path=TicketStatus}"/>
                 <DataGridComboBoxColumn Header="IT Person" x:Name="gITperson" Width="200"  SelectedValuePath="AssignTo" SelectedItemBinding="{Binding Path=AssignTo,Mode=TwoWay}"/>

    

             </DataGrid.Columns>
              
         </DataGrid>
     </ScrollViewer>

 </Grid>

</Window>

------------Xaml.cs------------------**

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;

namespace NTS
{
/// <summary>
/// Interaction logic for frmViewITT.xaml
/// </summary>
public partial class frmViewITT : Window
{
// ITPerson ITP = new ITPerson();


     SqlCommand Cmd;

     SqlDataReader reader;
     SqlConnection con = new SqlConnection(@"Connstr");
     private object gAssign;
     public List<String> gAss { get; set; } 
     public List<string> TicketSt { get; set; }          


     public frmViewITT()
     {
         InitializeComponent();
         FillData();
         //this.DataContext = ITP;

             TicketSt = new List<string>() { "Open", "Pending", "Close", };
        gTicketStatus.ItemsSource = TicketSt; 
              gAss = new List<string>();

         try
         {
                               
             con.Open();
             Cmd = con.CreateCommand();
             Cmd.CommandType = CommandType.Text;
             Cmd.CommandText = "Select * from ITPersons";
             Cmd.ExecuteNonQuery();
             DataTable dt = new DataTable();
             SqlDataAdapter da = new SqlDataAdapter(Cmd);
             da.Fill(dt);
                
             foreach (DataRow dr in dt.Rows)
             {
                   
                   
                 string nm = dr.Field<string>("Name");
                 gAss.Add(nm);
                 gITperson.ItemsSource = gAss;
             }                

             con.Close();
         }
         catch (Exception ex)
         {
             ex.Message.ToString();
         }

     }

     public void FillData()
     {

         con.Open();
         Cmd = con.CreateCommand();
         Cmd.CommandType = CommandType.Text;
         Cmd.CommandText = "Select * from Main";
         Cmd.ExecuteNonQuery();
         DataTable dt = new DataTable();
         SqlDataAdapter da = new SqlDataAdapter(Cmd);
         da.Fill(dt);

         grdEmp.ItemsSource = dt.DefaultView;

         con.Close();

     }

     private void GrdEmp_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
     {
            
     }
 }

}


windows-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@ GauravBatra-3507. Do you want to update the Selected Value of the last two ComboBox columns? Or all the contents of the selected row need to be updated?

0 Votes 0 ·

0 Answers