RadDropdownlist selected value not working

2 posts, 0 answers
  1. Habib
    Habib avatar
    1 posts
    Member since:
    Apr 2016

    Posted 10 Apr Link to this post

    hi, i use raddropdownlist to get value from raddatagrid when i click cell

    this my dropdownlist databind code

    private void showKategori()
            {
                try
                {
                    string query = "select ID_Kategori, Nama from Kategori";
                    SqlDataAdapter da = new SqlDataAdapter(query, con);
                    con.Open();
                    DataSet ds = new DataSet();
                    da.Fill(ds, "Kategori");
                    ddlkat.DisplayMember = "Nama";
                    ddlkat.ValueMember = "ID_Kategori";
                    ddlkat.DataSource = ds.Tables["Kategori"];
                    //ddlkat.Text = "Pilih Kategori";
                    con.Close();
                }
                catch (Exception ex)
                {
                    con.Close();
                    RadMessageBox.Show(ex.Message.ToString());
                }
            }

     

    and this my datagrid code

    try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("Select a.ID_Obat,a.ID_Kategori, b.Nama,a.ID_Supplier, c.Nama, a.Nama, a.Stok, a.Harga, a.Tanggal_Kadaluarsa,a.Tanggal_Masuk From Obat a JOIN Kategori b ON a.ID_Kategori=b.ID_Kategori JOIN Supplier c ON a.ID_Supplier=c.ID_Supplier", con);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    cmd.ExecuteNonQuery();
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        Griddataobat.DataSource = ds.Tables[0];
                        Griddataobat.Columns[0].HeaderText = "ID Obat";
                        Griddataobat.Columns[1].HeaderText = "IDKat";
                        Griddataobat.Columns[2].HeaderText = "Kategori";
                        Griddataobat.Columns[3].HeaderText = "IDSup";
                        Griddataobat.Columns[4].HeaderText = "Supplier";
                        Griddataobat.Columns[5].HeaderText = "Nama Obat";
                        Griddataobat.Columns[6].HeaderText = "Stok Obat";
                        Griddataobat.Columns[7].HeaderText = "Harga Satuan";
                        Griddataobat.Columns[8].HeaderText = "Tanggal Kadaluarsa";
                        Griddataobat.Columns[9].HeaderText = "Tanggal Obat Masuk";
                        Griddataobat.MasterGridViewTemplate.BestFitColumns();
                    }
                    con.Close();
                }
                catch(Exception ex)
                {
                    con.Close();
                    RadMessageBox.Show(ex.Message.ToString());
                }

     

     

    and this my datagrid when cell is click

    private void Griddataobat_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
            {
                txtnamaobat.Text = Griddataobat.Rows[e.RowIndex].Cells[0].Value.ToString();
                ddlkat.SelectedValue = Griddataobat.Rows[e.RowIndex].Cells[1].Value.ToString();
            }

     

     

    but my raddropdownlist not change after i click rad gridview

  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 12 Apr Link to this post

    Hello Habib,

    Thank you for writing.

    According to the provided code snippet, I suppose that the "ID_Kategori" is a numeric column. However, you set the RadDropDownList.SelectedValue property to a string value when clicking the cell. That is why the value is not selected. Here is a sample code snippet which result is illustrated at the attached gif file:
    public Form1()
    {
        InitializeComponent();
     
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Description", typeof(string));
     
        for (int i = 0; i < 20; i++)
        {
            dt.Rows.Add(i, "Name" + i, "Description" + i);
        }
     
        this.radGridView1.DataSource = dt;
        this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
     
        DataTable dt2 = new DataTable();
         
        dt2.Columns.Add("ItemId", typeof(int));
        dt2.Columns.Add("ItemName", typeof(string));
        for (int i = 0; i < 20; i++)
        {
            dt2.Rows.Add(i, "Name" + i);
        }
     
        this.radDropDownList1.DataSource = dt2;
        this.radDropDownList1.DisplayMember = "ItemName";
        this.radDropDownList1.ValueMember = "ItemId";
     
        this.radGridView1.CellClick += radGridView1_CellClick;
    }
     
    private void radGridView1_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
    {
        this.radDropDownList1.SelectedValue = this.radGridView1.Rows[e.RowIndex].Cells["Id"].Value;
    }

    Note that the SelectedValue property should be set to a valid value considering the ValueMember  property and its type.

    I hope this information helps. Should you have further questions I would be glad to help.

    Regards,
    Dess
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top