how can fill data in tree view from sql ?
  • HI,i want to bind my sql table data in tree view kindly help how.
  • 1 Comment sorted by
  • Vote Up0Vote Down WaqasWaqas
    172.00 Karma Accepted Answer
    Hi call CreateTreeView function on button click or form load.code is below.





    public void CreateTreeView(TreeView trw)
    {
    try
    {
    trw.Nodes.Clear();
    DataTable dt = objSer.GetDataTable("Select AccName,AccCode,BranchCode from tblChartofAccount where (HeadAccCode is null or HeadAccCode = '') and IsNull(IsDeleted,0)=0");

    foreach (DataRow dr in dt.Rows)
    {
    System.Windows.Forms.TreeNode trn = new System.Windows.Forms.TreeNode();
    trn.Text = dr["AccName"].ToString();
    trn.Tag = dr["AccCode"].ToString();
    trn.Name = dr["BranchCode"].ToString();
    AddChileNode(trn);
    trw.Nodes.Add(trn);
    }
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message.ToString());
    return;
    }
    }
    public void AddChileNode(TreeNode trn)
    {
    try
    {
    DataTable dt = objSer.GetDataTable("declare @HeadAccCode varchar(50) declare @CompanyCode varchar(50) declare @BranchCode varchar(50) set @HeadAccCode='" + trn.Tag.ToString() + "' set @CompanyCode='" + log.GetCompanyCode() + "' set @BranchCode='" + log.GetBranchCode() + "' Select AccName,AccCode,BranchCode from tblChartofaccount where HeadAccCode = @HeadAccCode and CompanyCode=@CompanyCode and BranchCode=@BranchCode and IsNull(IsDeleted,0)=0");

    foreach (DataRow drChild in dt.Rows)
    {
    TreeNode trnchild = new TreeNode();
    trnchild.Text = drChild["AccName"].ToString();
    trnchild.Tag = drChild["AccCode"].ToString();
    trnchild.Name = drChild["BranchCode"].ToString();
    AddChileNode(trnchild);
    trn.Nodes.Add(trnchild);
    }

    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message.ToString());
    }
    }

    public DataTable GetDataTable(string Query)
    {

    SqlTransaction objTran;//= new SqlTransaction();
    SqlConnection objCon = new SqlConnection("Your Connection String");
    SqlCommand objCmd = new SqlCommand(Query, objCon);

    SqlDataAdapter objDA = new SqlDataAdapter(objCmd);// = new SqlDataAdapter(Query, objCon, objConStr.GetConnStr);

    objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
    DataTable dt = new DataTable("Table");
    try
    {
    objDA.Fill(dt);
    }
    catch (Exception ex)
    {

    }
    return dt;

    }


Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week