Click here to Skip to main content
15,902,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i tried to convert from binding source to data table ...but every time i'm getting and error, please help.
C#
objMedSource.DataSource = objMedication;
            dtgMedicine.DataSource = objMedSource;//where i showed the binding source to be the data source of data grid
   DataTable dttable;
            //DataView dv;
            //dv = (DataView) dtgMedicine.DataSource;
            //dv.DataViewManager = objMedSource;
            //dttable = dv.Table.DataSet.Tables[0];
            //dttable = ((DataSet)objMedSource.DataSource).Tables[objMedSource.DataMember];
            //dttable = (DataTable)objMedSource.DataSource;
            dttable = ((DataTable)(dtgMedicine.DataSource)).Clone();

here i get error during conversion..i tried several possible ways. please help
Posted
Updated 4-Dec-11 16:48pm
v2
Comments
[no name] 4-Dec-11 22:47pm    
what specific error are you getting?
Kutub1987 4-Dec-11 22:53pm    
using this dttable = ((DataTable)(dtgMedicine.DataSource)).Clone();
i have the following error : -
Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'.

1 solution

Do you mean you want to load data into the Datatable from an ObjectDataSource? If so, technically it's possible but pointless. The role of the ObjectDataSource is to declaratively bind data to a control. You would need to create a method to load it into something (such as a DataTable, or more likely a List<myobject></myobject>) before the ObjectDataSource control could work with it. ObjectdataSource controls are not data containers. They are simply a means of pointing to a method that returns data in one form or another. REF

If you are looking for Connecting an ObjectDataSource to a DataTable then try the following:
Connecting an ObjectDataSource to a DataTable in Code-behind
 
Share this answer
 
Comments
Kutub1987 5-Dec-11 3:50am    
Actually i have used binding source to display some information in the data grid view as data in the view might be changed(a row might be deleted or column of a row might be edited) by the user. Now when i want to save the data i cant access through data grid views' data source, so i tried this way. Please let me know if you have any suggestions.
Monjurul Habib 5-Dec-11 4:21am    
share your full code including data binding.
Kutub1987 5-Dec-11 10:53am    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PatientPrescription.DAO.DAL;
using PatientPrescription.UI;
using PatientPrescription.DAO.GATEWAY;
using PatientPrescription.Utility;
using PatientPrescription.BLL;

namespace PatientPrescription.UI
{
public partial class MedicationUI : Form
{
AppMessage appMes;
MedicationGateway objectCls ;
public string testType, testName;
Patient patientMed;
string type;
private List<object> listMedName;
private List<medication> objMedication;
private List<Object> objdeletedMed;
private int _patientID;
private int _prescriptionID;
private BindingSource objMedSource = new BindingSource();
public MedicationUI()
{
objectCls = new MedicationGateway();
InitializeComponent();
FillTreeView();
}
public MedicationUI(Patient patient, string typeFrom,int patinetID,int PrescriptionID)
{
_patientID = patinetID;
_prescriptionID = PrescriptionID;
objectCls = new MedicationGateway();
patientMed = new Patient();
patientMed = patient;
type = typeFrom;
InitializeComponent();
FillTreeView();
}
private void btnClose_Click(object sender, EventArgs e)
{
if (appMes.Id == -2)
{
DialogResult answer = MessageBox.Show("You Didn't Save/Enter Any Medicine, Really Want to Close?", "Attention", MessageBoxButtons.YesNo);
if (answer == DialogResult.Yes)
{
this.Close();
PrescriptionUI pUI = new PrescriptionUI(patientMed, "medicine");
pUI.Show();
}
}
else
{
PrescriptionUI prescriptionUI = new PrescriptionUI(patientMed, "medicine");
prescriptionUI.Show();
this.Close();
}
}

private void btnSave_Click(object sender, EventArgs e)
{
listMedName = new List<object>();
foreach (TreeNode trNode in trvMedicine.Nodes)
{
if (trNode.Checked == true)
{
foreach (TreeNode childNode in trNode.Nodes)
{
if (childNode.Checked == true)
{
listMedName.Add(childNode.Text);
}
}
}
else
{
MessageBox.Show("No Parent Medicine was selected","Error",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
return;
}
}
objMedication = new List<medication>();
objMedication = objectCls.GetSelectedMedicine(listMedName);

objMedSource.DataSource = objMedication;
dtgMedicine.DataSource = objMedSource;
dtgMedicine.Columns["ID"].Visible = false;
dtgMedicine.Columns["PID"].Visible = false;
dtgMedicine.Columns["DoseID"].Visible = false;
dtgMedicine.Columns["TypeID"].Visible = false;
dtgMedicine.Columns["TypeDesc"].ReadOnly = true;
dtgMedicine.Columns["Name"].ReadOnly = true;
}
private void lnkClickToAddMore_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{

MedicationListSetupUI objMedicationListSetupUI = new MedicationListSetupUI();
objMedicationListSetupUI.Show();
//this.Close();
}
public void FillTreeView()
{
DataSet dsTreeViewParents;
DataSe

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900