Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

how to get checkbox checked items in datagrid in wpf

$
0
0

hi ,

   I want to get checked items values..i dont know how to get please help me..

<Grid >
        <GroupBox Header="Payslip Details" Name="lvpayslipdetails" Margin="0,66,0,36">
            <grid:DataGrid x:Name="lvpayslip" x:Uid="MyDataGrid" AutoGenerateColumns="False"
AlternationCount="1" SelectionMode="Single" ItemsSource="{Binding Path=Table}" IsReadOnly="True" HeadersVisibility="Column" FontSize="14" >

                <grid:DataGrid.Columns>
                    <grid:DataGridTemplateColumn >
                        <grid:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox />
                            </DataTemplate>
                        </grid:DataGridTemplateColumn.CellTemplate>
                    </grid:DataGridTemplateColumn>

                    
                    <grid:DataGridTextColumn Binding="{Binding Path=p_id}" IsReadOnly="True"
Header="P.Id" Width="0.7*" />

                    <grid:DataGridTextColumn Binding="{Binding Path=dbempname}"
Header="Emp Name" Width="2*" IsReadOnly="True" />
                    <grid:DataGridTextColumn Binding="{Binding Path=pmonth}"
Header="Month" Width="0.9*" IsReadOnly="True" />
                    <grid:DataGridTextColumn Binding="{Binding Path=pyear}"
Header="Year" Width="0.5*" IsReadOnly="True" />

                   

                    <grid:DataGridTextColumn Binding="{Binding Path=gross,StringFormat=0.00}"
Header="Gross" Width="1*" IsReadOnly="True" />


                    <grid:DataGridTextColumn Binding="{Binding Path=deduction,StringFormat=0.00}"
Header="Deduction" Width="1.5*" IsReadOnly="True" />

                    <grid:DataGridTextColumn Binding="{Binding Path=netpay,StringFormat=0.00}"
Header="Net Pay" Width="1.3*" IsReadOnly="True" />

                    <grid:DataGridTemplateColumn Header="View Profile" Width="0.9*">

                        <grid:DataGridTemplateColumn.CellTemplate>

                            <DataTemplate>

                                <Button Content="View" Click="EditButton_Click" Template="{StaticResource view}"  >

                                </Button>

                            </DataTemplate>

                        </grid:DataGridTemplateColumn.CellTemplate>

                    </grid:DataGridTemplateColumn>


                </grid:DataGrid.Columns>

            </grid:DataGrid>

        </GroupBox>
        <Button Content="view" Height="23" HorizontalAlignment="Left" Margin="378,37,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>

and C# code...

public payslip()
        {
            InitializeComponent();
        }

        SqlConnection con;
        public bool flag;
        string cn = ConfigurationManager.AppSettings["cc"];

        public void viewleave()
        {
            con = new SqlConnection(cn);
            con.Open();
            SqlCommand cmd = new SqlCommand("lvpayslip", con);
            cmd.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            lvpayslip.DataContext = dt.DefaultView;
            con.Close();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            viewleave();
        }

        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            report r = new report();
            con = new SqlConnection(cn);
            con.Open();
            SqlCommand cmd = new SqlCommand("viewpayslip", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@pid", (int)((DataRowView)lvpayslip.SelectedItems[0])["p_id"]);
            cmd.Parameters.AddWithValue("@month", (String)((DataRowView)lvpayslip.SelectedItems[0])["pmonth"]);
            cmd.Parameters.AddWithValue("@year", (int)((DataRowView)lvpayslip.SelectedItems[0])["pyear"]);
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            r.viewpay(dt, (String)((DataRowView)lvpayslip.SelectedItems[0])["pmonth"], (int)((DataRowView)lvpayslip.SelectedItems[0])["pyear"]);
            r.Show();
            con.Close();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            foreach (datagridrow row in lvpayslip.Rows)
            {
                if ((bool)(Row.Cells["chkBxSelect"].Value) == true)
                {
                    this.dgvSelectAll.Rows[Row.Index].Selected = true;
                }
                else
                {
                    this.dgvSelectAll.Rows[Row.Index].Selected = false;
                }
            }
        }

now i want to checked some checkbox then i click that view button ...it will show all selected items(only p_id colums)in message  box i dont have any idea ...but i used some code from net i got an error as  ....Error    1   'Microsoft.Windows.Controls.DataGrid' does not contain a definition for 'Rows' and no extension method 'Rows' accepting a first argument of type 'Microsoft.Windows.Controls.DataGrid' could be found (are you missing a using directive or an assembly reference?)


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>