コード例 #1
0
        private void btnIzaberi_Click(object sender, EventArgs e)
        {
            if (dgvPregledLookUp.Rows.Count == 0)
            {
                MessageBox.Show("Nema slobodnih mjesta");
                this.Close();
                return;
            }
            DataGridViewRow row        = dgvPregledLookUp.SelectedRows[0];
            var             properties = myProperty.GetType().GetProperties();

            string columnName = properties.Where(x => x.GetCustomAttribute <LookUpKeyAttribute>() != null)
                                .FirstOrDefault().GetCustomAttribute <SqlNameAttribute>().Naziv;

            Key = row.Cells[columnName].Value.ToString();

            columnName = properties.Where(x => x.GetCustomAttribute <LookUpValueAttribute>() != null)
                         .FirstOrDefault().GetCustomAttribute <SqlNameAttribute>().Naziv;

            if (columnName.Contains("ID"))
            {
                PropertyInterface foreignKeyInterface = Assembly.GetExecutingAssembly().
                                                        CreateInstance(properties.Where(x => x.GetCustomAttribute <ForeignKeyAttribute>() != null)
                                                                       .FirstOrDefault().GetCustomAttribute <ForeignKeyAttribute>().className)
                                                        as PropertyInterface;

                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.GetConnectionString(), CommandType.Text,
                                                               foreignKeyInterface.GetLookUpQuery(row.Cells[1].Value.ToString()));

                DataTable dt = new DataTable();
                dt.Load(reader);
                reader.Close();

                dgvPregledLookUp.DataSource = dt;

                row = dgvPregledLookUp.Rows[0];

                Value = row.Cells[0].Value.ToString();
            }
            else
            {
                Value = row.Cells[columnName].Value.ToString();
            }

            this.Close();
        }