如何在一个gridview中绑定两个数据表? [英] How to bind two datatables in the one gridview?

查看:59
本文介绍了如何在一个gridview中绑定两个数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码下楼了,但它向我显示并显示错误,提示未分配的局部变量值"

I have my code down stairs but it showing me and error saying unassigned local variable ''values''

private void creategvreceptionist()
    {
        //int value = 0;
        //DataTable mdt;
        //DataRow mrow;
        string[] values;
        //string[] values1;
        DataTable dtreceptionisttmslot = (DataTable)cnt.GetRecord("select first_appointment_time , tmslot_id from tblValidAppointmentTime ");

        foreach (DataRow row in dtreceptionisttmslot.Rows)
        {

            
            string q = "select v.first_appointment_time,v.tmslot_id,e.pc_eventDate,p.DOB,p.fname,p.lname,e.pc_eid FROM tblValidAppointmentTime As v,tblPatientData as p, tblOpenemrPostcalendarEvents as e where e.pc_eventDate>='"+DateTime.Now.ToShortDateString()+"'";
            DataTable dtreceptionist = (DataTable)cnt.GetRecord(q);

            if (dtreceptionist.Rows.Count > 0)
            {                
                foreach (DataRow arow in dtreceptionist.Rows)
                {
                   //DataRow brow = dtreceptionist.NewRow();
                   //arow["fname"] = dtreceptionist.Rows[0][1];
                   //brow["patient name"] = dtreceptionist.Rows[0][2];               
                    
                   values[0] = row["fname"].ToString();
                   values[1]= arow["patient name"].ToString();
                   values[2]= arow["DOB"].ToString();

                   dtreceptionisttmslot.Rows.Add(arow);
                }
            }
            else
            {
                //Values[0] = row["time"].ToString();
                //Values[1] = null;
                //Values[2] = null;
                //dtreceptionisttmslot.Rows.Add();
            }
        }
            dtreceptionisttmslot.AcceptChanges();
            gvreceptionist.DataSource = dtreceptionisttmslot;
            gvreceptionist.DataBind();
        }


有人对此有解决方案吗?

谢谢


does anybody have solution for this?

Thank you

推荐答案

如果您想将网格与两个表绑定,那么首先您应该将两个表合并到一个表中,但是两个表的合并结构应该相同.那么你可以尝试这个
//用于合并表
foreach(表1.Rows中的DataRow dr)
{
table2.Rows.Add(dr.ItemArray);
}


合并表格后,您可以绑定网格

gridview1.DataSource = table2;
grdview1.DataBind();

希望这对你有帮助.
谢谢
if u want to bind the grid with two table then firstly u should merge both table in single table but for merging structure of both table should be same .then u can try this
// for merging tables
foreach (DataRow dr in table1.Rows)
{
table2.Rows.Add(dr.ItemArray);
}


After merging tables u can bind grid

gridview1.DataSource=table2;
grdview1.DataBind();

Hope this will help u.
Thanks


您写道:
string[] values;


发布后,您可以直接使用它:


Post that, you directly use it:

values[0] = row["fname"].ToString();
values[1]= arow["patient name"].ToString();
values[2]= arow["DOB"].ToString();


您何时初始化变量?您什么时候写的,它将保留3个值?

您需要先定义变量,然后再开始使用它并分配值.


When did you initialize the variable? When did you wrote it is going to hold 3 value?

You need to define the variable before starting to use it and assign values.


添加到Sandeep的答案中,更改操作顺序,以使初始化工作得更好. br/> 您需要先获取数据,然后才能将数组初始化为所需的大小.
Adding to Sandeep''s answer, change the order in which you do things so that your initialization works better.
You need to get your data before you can initialize the array to its required size.
string[] values;
DataTable dtreceptionisttmslot = (DataTable)cnt.GetRecord("select first_appointment_time from tblValidAppointmentTime ");

if( dtreceptionisttmslot != null && dtreceptionisttmslot.Rows.Count > 0 )
{
   values = new string[dtreceptionisttmslot.rows.count];
   // ... Now do your processing
}


我个人认为这样做会更好


My personal take is that this would work better

List<string> values = new List<string>();
DataTable dtreceptionisttmslot = (DataTable)cnt.GetRecord("select first_appointment_time from tblValidAppointmentTime ");
foreach( DataRow dr in dtreceptionisttmslot.rows )
{
   // Do your processing and when you are ready to add a new item into the list do this
   values.Add( yourNewValue );
}


这篇关于如何在一个gridview中绑定两个数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆