SqlConnection srcConnection = new SqlConnection();
SqlCommand sqloldbranch = new SqlCommand();
SqlDataAdapter daoldbranch = new SqlDataAdapter();
DataTable dtoldbranch = new DataTable();
ConnString = ConfigurationManager.ConnectionStrings["connstr"].ToString();
srcConnection.ConnectionString = ConnString;
sqloldbranch.Connection = srcConnection;
sqloldbranch.CommandText = "select 商场代码,商场名称 from test";
sqloldbranch.CommandType = CommandType.Text;
sqloldbranch.Connection.Open();
daoldbranch.SelectCommand = sqloldbranch;
daoldbranch.Fill(dtoldbranch);
sqloldbranch.Connection.Close();
// for (int j = 0; j < dtoldbranch.Rows.Count; j++)
// {
// string branchnum = dtoldbranch.Rows[i].ToString();
// }
for (int i = 0; i < GVBranch.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GVBranch.Rows[i].FindControl("CheckBox1");
if (GVBranch.Rows[i].Cells[1].Text.Trim().ToString() == "XXX")//怎么把dtoldbranch里的值放在这里进行判断呢?dtoldbranch里的值不止一行 {
cbox.Checked = true;
}
}
11 个解决方案
#1
dtoldbranch里的值和GVBranch 一样么?
#2
把dgv那行值取出来
用dt.Rows.Contains(dgv)来判断
用dt.Rows.Contains(dgv)来判断
#3
不一样,dtoldbranch的值肯定比GVBranch 的值要少
#4
试试去
#5
string ConnString = "";
SqlConnection srcConnection = new SqlConnection();
SqlCommand sqloldbranch = new SqlCommand();
SqlDataAdapter daoldbranch = new SqlDataAdapter();
DataTable dtoldbranch = new DataTable();
ConnString = ConfigurationManager.ConnectionStrings["connstr"].ToString();
srcConnection.ConnectionString = ConnString;
sqloldbranch.Connection = srcConnection;
sqloldbranch.CommandText = "select 商场代码,商场名称 from test";
sqloldbranch.CommandType = CommandType.Text;
sqloldbranch.Connection.Open();
daoldbranch.SelectCommand = sqloldbranch;
daoldbranch.Fill(dtoldbranch);
sqloldbranch.Connection.Close();
DataColumn[] keys = new DataColumn[1];
keys[0] = dtoldbranch.Columns[1];
dtoldbranch.PrimaryKey = keys;
for (int i = 0; i < GVBranch.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GVBranch.Rows[i].FindControl("CheckBox1");
string branchnum = dtoldbranch.Rows[i]["商场代码"].ToString();
if (GVBranch.Rows[i].Cells[1].Text.Trim().ToString().Contains(branchnum))
{
cbox.Checked = true;
}
}
这样改完后有几个问题,GVBranch.Rows.Count固定是40,dtoldbranch.Rows.Count不固定,但是肯定会小于40,当i大于dtoldbranch.Rows.Count后就会出错
SqlConnection srcConnection = new SqlConnection();
SqlCommand sqloldbranch = new SqlCommand();
SqlDataAdapter daoldbranch = new SqlDataAdapter();
DataTable dtoldbranch = new DataTable();
ConnString = ConfigurationManager.ConnectionStrings["connstr"].ToString();
srcConnection.ConnectionString = ConnString;
sqloldbranch.Connection = srcConnection;
sqloldbranch.CommandText = "select 商场代码,商场名称 from test";
sqloldbranch.CommandType = CommandType.Text;
sqloldbranch.Connection.Open();
daoldbranch.SelectCommand = sqloldbranch;
daoldbranch.Fill(dtoldbranch);
sqloldbranch.Connection.Close();
DataColumn[] keys = new DataColumn[1];
keys[0] = dtoldbranch.Columns[1];
dtoldbranch.PrimaryKey = keys;
for (int i = 0; i < GVBranch.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GVBranch.Rows[i].FindControl("CheckBox1");
string branchnum = dtoldbranch.Rows[i]["商场代码"].ToString();
if (GVBranch.Rows[i].Cells[1].Text.Trim().ToString().Contains(branchnum))
{
cbox.Checked = true;
}
}
这样改完后有几个问题,GVBranch.Rows.Count固定是40,dtoldbranch.Rows.Count不固定,但是肯定会小于40,当i大于dtoldbranch.Rows.Count后就会出错
#6
不是吧,dtoldbranch是DataTable。他的count肯定比GVBranch.Count大啊。
不管dtoldbranch.Rows.Count固定或者不固定,GVBranch的数据源是dtoldbranch
i会大于dtoldbranch.Rows.Count吗?
#7
我试试看把gvbranch和dtoldbranch反写行不行
#8
真不行的话 吧GDV的那个数据取出来,作为条件去取 datatable,只要存在不就行了么
#9
反写真不行,就是怎么循环的问题
#10
dtoldbranch只有两行,gvbranch有40行
举例说
gvbranch的第3行和dtoldbranch的第一行能对应上
gvbranch的第35行和dtoldbranch的第二行能对应上
这样的循环怎么写啊
举例说
gvbranch的第3行和dtoldbranch的第一行能对应上
gvbranch的第35行和dtoldbranch的第二行能对应上
这样的循环怎么写啊
#11
终于搞定了,其实就是个很简单的嵌套循环,汗!!!!!!!!
for (int i = 0; i < GVBranch.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GVBranch.Rows[i].FindControl("CheckBox1");
for (int j = 0; j < dtoldbranch.Rows.Count; j++)
{
string branchnum = dtoldbranch.Rows[j]["商场代码"].ToString();
string gvbranchnum = GVBranch.Rows[i].Cells[1].Text.Trim().ToString();
if (branchnum.Contains(gvbranchnum))
{
cbox.Checked = true;
}
}
}
for (int i = 0; i < GVBranch.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GVBranch.Rows[i].FindControl("CheckBox1");
for (int j = 0; j < dtoldbranch.Rows.Count; j++)
{
string branchnum = dtoldbranch.Rows[j]["商场代码"].ToString();
string gvbranchnum = GVBranch.Rows[i].Cells[1].Text.Trim().ToString();
if (branchnum.Contains(gvbranchnum))
{
cbox.Checked = true;
}
}
}
#1
dtoldbranch里的值和GVBranch 一样么?
#2
把dgv那行值取出来
用dt.Rows.Contains(dgv)来判断
用dt.Rows.Contains(dgv)来判断
#3
不一样,dtoldbranch的值肯定比GVBranch 的值要少
#4
试试去
#5
string ConnString = "";
SqlConnection srcConnection = new SqlConnection();
SqlCommand sqloldbranch = new SqlCommand();
SqlDataAdapter daoldbranch = new SqlDataAdapter();
DataTable dtoldbranch = new DataTable();
ConnString = ConfigurationManager.ConnectionStrings["connstr"].ToString();
srcConnection.ConnectionString = ConnString;
sqloldbranch.Connection = srcConnection;
sqloldbranch.CommandText = "select 商场代码,商场名称 from test";
sqloldbranch.CommandType = CommandType.Text;
sqloldbranch.Connection.Open();
daoldbranch.SelectCommand = sqloldbranch;
daoldbranch.Fill(dtoldbranch);
sqloldbranch.Connection.Close();
DataColumn[] keys = new DataColumn[1];
keys[0] = dtoldbranch.Columns[1];
dtoldbranch.PrimaryKey = keys;
for (int i = 0; i < GVBranch.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GVBranch.Rows[i].FindControl("CheckBox1");
string branchnum = dtoldbranch.Rows[i]["商场代码"].ToString();
if (GVBranch.Rows[i].Cells[1].Text.Trim().ToString().Contains(branchnum))
{
cbox.Checked = true;
}
}
这样改完后有几个问题,GVBranch.Rows.Count固定是40,dtoldbranch.Rows.Count不固定,但是肯定会小于40,当i大于dtoldbranch.Rows.Count后就会出错
SqlConnection srcConnection = new SqlConnection();
SqlCommand sqloldbranch = new SqlCommand();
SqlDataAdapter daoldbranch = new SqlDataAdapter();
DataTable dtoldbranch = new DataTable();
ConnString = ConfigurationManager.ConnectionStrings["connstr"].ToString();
srcConnection.ConnectionString = ConnString;
sqloldbranch.Connection = srcConnection;
sqloldbranch.CommandText = "select 商场代码,商场名称 from test";
sqloldbranch.CommandType = CommandType.Text;
sqloldbranch.Connection.Open();
daoldbranch.SelectCommand = sqloldbranch;
daoldbranch.Fill(dtoldbranch);
sqloldbranch.Connection.Close();
DataColumn[] keys = new DataColumn[1];
keys[0] = dtoldbranch.Columns[1];
dtoldbranch.PrimaryKey = keys;
for (int i = 0; i < GVBranch.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GVBranch.Rows[i].FindControl("CheckBox1");
string branchnum = dtoldbranch.Rows[i]["商场代码"].ToString();
if (GVBranch.Rows[i].Cells[1].Text.Trim().ToString().Contains(branchnum))
{
cbox.Checked = true;
}
}
这样改完后有几个问题,GVBranch.Rows.Count固定是40,dtoldbranch.Rows.Count不固定,但是肯定会小于40,当i大于dtoldbranch.Rows.Count后就会出错
#6
不是吧,dtoldbranch是DataTable。他的count肯定比GVBranch.Count大啊。
不管dtoldbranch.Rows.Count固定或者不固定,GVBranch的数据源是dtoldbranch
i会大于dtoldbranch.Rows.Count吗?
#7
我试试看把gvbranch和dtoldbranch反写行不行
#8
真不行的话 吧GDV的那个数据取出来,作为条件去取 datatable,只要存在不就行了么
#9
反写真不行,就是怎么循环的问题
#10
dtoldbranch只有两行,gvbranch有40行
举例说
gvbranch的第3行和dtoldbranch的第一行能对应上
gvbranch的第35行和dtoldbranch的第二行能对应上
这样的循环怎么写啊
举例说
gvbranch的第3行和dtoldbranch的第一行能对应上
gvbranch的第35行和dtoldbranch的第二行能对应上
这样的循环怎么写啊
#11
终于搞定了,其实就是个很简单的嵌套循环,汗!!!!!!!!
for (int i = 0; i < GVBranch.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GVBranch.Rows[i].FindControl("CheckBox1");
for (int j = 0; j < dtoldbranch.Rows.Count; j++)
{
string branchnum = dtoldbranch.Rows[j]["商场代码"].ToString();
string gvbranchnum = GVBranch.Rows[i].Cells[1].Text.Trim().ToString();
if (branchnum.Contains(gvbranchnum))
{
cbox.Checked = true;
}
}
}
for (int i = 0; i < GVBranch.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GVBranch.Rows[i].FindControl("CheckBox1");
for (int j = 0; j < dtoldbranch.Rows.Count; j++)
{
string branchnum = dtoldbranch.Rows[j]["商场代码"].ToString();
string gvbranchnum = GVBranch.Rows[i].Cells[1].Text.Trim().ToString();
if (branchnum.Contains(gvbranchnum))
{
cbox.Checked = true;
}
}
}