Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
3.18/5 (3 votes)
See more:
Hello everyone,
i am having gridview which displays name,code,etc....in which i have one field called 'date of leave' which has multiple values.so i am displaying dates as template field in gridview.For that i am getting dates for perticuler name into datatable and now i want to display the whole datable.I am not able to figure this out,but can anyone suggest me the way to display whole datatable in label? Below is the code i have used:


C#
SqlCommand cmd4 = new SqlCommand("select date1 from leave_rec where name='" + ename + "' AND MONTH(date1)='" + DropDownList3.SelectedValue + "' ", conn);

                SqlDataAdapter da4=new SqlDataAdapter(cmd4);
                DataTable dt=new DataTable();
                da4.Fill(dt);
                Label labd = (Label)e.Row.FindControl("dates");
                labd.Text = dt;/////here i want to assign label text.


In the above code,'dates' is the id of the label which is defined in gridview as templatefield.
Posted
Updated 7-Mar-13 23:10pm
v4
Comments
Maciej Los 8-Mar-13 5:16am    
Whole database in ibe label???
You can't assign a datatable as a source of text for label! Datatable is an object and you need to assign text for every single datafield in record member.

Even if you could do it - which you can, but it takes rather more work than that - you do realize that the data will be displayed without any formatting? Or line breaks, unless you specifically add them?

I don't think that is what you want to do, so I won't waste time by coding it for you, but all you have to do is loop through each row in the DataTable, then through each cell in the row and add the Value property data to the label text as a string. It'll look horrible though!
 
Share this answer
 
Comments
Thanks7872 8-Mar-13 5:25am    
i want this because,in my gridview i have fields named name,code,balance,total leaves availed this year,,dates..now all the values except dates have unique values,so i want to display them only once per name,that is ORIGINALGRIFF,021012,25,9,?,at the last field there will be 9 dates,right?so they should be displayed,how to overcome this?if want more clarification on the same let me know...please help me as its so urgent for me......
Hi,
please look into follwoing code ,may be it can help u to fix your issue.
C#
protected void gvdetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            #region Getting Data from database
            DBConnection bd = new DBConnection();
            DataSet ds = new DataSet();
            ds = bd.FromDatabase("Select VendorId from DailyIncome");
            #endregion
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string temp = "";
                Label LblError = (Label)e.Row.FindControl("lblShow");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {

                    temp += ds.Tables[0].Rows[i][0].ToString();

                }
                LblError.Text = temp;

            }
        }


jmd:-)
 
Share this answer
 

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