C# 如何从另一个datagridview创建datagridview?
我使用的是visual studio 2012:c#winform。我有一个C# 如何从另一个datagridview创建datagridview?,c#,visual-studio-2012,datagridview,C#,Visual Studio 2012,Datagridview,我使用的是visual studio 2012:c#winform。我有一个datagridview,其中创建了两列(Id和society)。datagridview包含从我的数据库中获取的25条记录 我还在datagridview 注意:在我的phpmyadmin数据库中,每个社团都包含许多服务类别和服务类型 我的问题是: 我希望当按下按钮“action”时,与该社会相关的所有服务类别和服务类型都会显示在同一个或另一个datagridview中 您能帮助我吗?尝试类似的操作,您必须使用主Dat
datagridview
,其中创建了两列(Id和society)。datagridview
包含从我的数据库中获取的25条记录
我还在datagridview
注意:在我的phpmyadmin
数据库中,每个社团都包含许多服务类别和服务类型
我的问题是:
我希望当按下按钮“action”时,与该社会相关的所有服务类别和服务类型都会显示在同一个或另一个datagridview
中
您能帮助我吗?尝试类似的操作,您必须使用主DataGridView启动此函数,然后您将拥有“克隆”DataGridView
public DataGridView CloneDataGrid(DataGridView mainDataGridView)
{
DataGridView cloneDataGridView = new DataGridView();
if (cloneDataGridView.Columns.Count == 0)
{
foreach (DataGridViewColumn datagrid in mainDataGridView.Columns)
{
cloneDataGridView.Columns.Add(datagrid.Clone() as DataGridViewColumn);
}
}
DataGridViewRow dataRow = new DataGridViewRow();
for (int i = 0; i < mainDataGridView.Rows.Count; i++)
{
dataRow = (DataGridViewRow)mainDataGridView.Rows[i].Clone();
int Index = 0;
foreach (DataGridViewCell cell in mainDataGridView.Rows[i].Cells)
{
dataRow.Cells[Index].Value = cell.Value;
Index++;
}
cloneDataGridView.Rows.Add(dataRow);
}
cloneDataGridView.AllowUserToAddRows = false;
cloneDataGridView.Refresh();
return cloneDataGridView;
}
公共DataGridView克隆数据网格(DataGridView主DataGridView)
{
DataGridView cloneDataGridView=新DataGridView();
if(cloneDataGridView.Columns.Count==0)
{
foreach(mainDataGridView.Columns中的DataGridViewColumn datagrid)
{
cloneDataGridView.Columns.Add(datagrid.Clone()作为DataGridViewColumn);
}
}
DataGridViewRow dataRow=新DataGridViewRow();
对于(int i=0;i希望获得此帮助尝试类似的操作,您必须使用主DataGridView启动此函数,然后您将拥有“克隆”DataGridView
public DataGridView CloneDataGrid(DataGridView mainDataGridView)
{
DataGridView cloneDataGridView = new DataGridView();
if (cloneDataGridView.Columns.Count == 0)
{
foreach (DataGridViewColumn datagrid in mainDataGridView.Columns)
{
cloneDataGridView.Columns.Add(datagrid.Clone() as DataGridViewColumn);
}
}
DataGridViewRow dataRow = new DataGridViewRow();
for (int i = 0; i < mainDataGridView.Rows.Count; i++)
{
dataRow = (DataGridViewRow)mainDataGridView.Rows[i].Clone();
int Index = 0;
foreach (DataGridViewCell cell in mainDataGridView.Rows[i].Cells)
{
dataRow.Cells[Index].Value = cell.Value;
Index++;
}
cloneDataGridView.Rows.Add(dataRow);
}
cloneDataGridView.AllowUserToAddRows = false;
cloneDataGridView.Refresh();
return cloneDataGridView;
}
公共DataGridView克隆数据网格(DataGridView主DataGridView)
{
DataGridView cloneDataGridView=新DataGridView();
if(cloneDataGridView.Columns.Count==0)
{
foreach(mainDataGridView.Columns中的DataGridViewColumn datagrid)
{
cloneDataGridView.Columns.Add(datagrid.Clone()作为DataGridViewColumn);
}
}
DataGridViewRow dataRow=新DataGridViewRow();
对于(int i=0;i希望此帮助如果我正确理解您的问题,您可以使用单元格内容单击事件用过滤结果重新填充数据表 我已经在我的一个表单上完成了这项工作(见下文),但是我没有使用phpmyadmin数据库,只有sql,但是逻辑可能是相同的 我还应该注意,我的默认单元格选择为“整行选择” 为了参数起见,假设表单显示产品,那么ProductID、ProductName和ProductType。单击网格中的按钮时,我们希望它按所选行的ProductType进行过滤
//First we declare a static string that can be accessed by the whole form
//and blank it so the form doesnt use a previous value.
public static string product_type = ""
//In the form load event, the gridview is populated - this could be moved to anywhere.
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("DB connection string");
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Products", conn);
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sqlDataAdap.Fill(ds);
DataTable dt = new DataTable();
sqlDataAdap.Fill(dt);
conn.Close();
}
//In the cell contect click event we give it the ProductType
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//Here we declare a var to identify that we're looking at the row
var row = dataGridView1.CurrentRow.Cells;
//Now we set the string we declared earlier
product_type = Convert.ToString(row["ProductType"].Value);
//Now we repeat the grid script with a parameter which uses our string from above
SqlConnection conn = new SqlConnection("DB connection string");
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Products WHERE ProductType = @ProductType", conn);
sc.Parameters.Add("@ProductType", SqlDbType.Int).Value = product_type;
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sqlDataAdap.Fill(ds);
DataTable dt = new DataTable();
sqlDataAdap.Fill(dt);
conn.Close();
}
然后,您可以添加一个按钮,当单击该按钮将网格返回到非过滤状态时,该按钮会重复表单加载事件中的脚本
希望这有帮助。如果我正确理解了您的问题,您可以使用单元格内容单击事件用过滤结果重新填充数据表 我已经在我的一个表单上完成了这项工作(见下文),但是我没有使用phpmyadmin数据库,只有sql,但是逻辑可能是相同的 我还应该注意,我的默认单元格选择为“整行选择” 为了参数起见,假设表单显示产品,那么ProductID、ProductName和ProductType。单击网格中的按钮时,我们希望它按所选行的ProductType进行过滤
//First we declare a static string that can be accessed by the whole form
//and blank it so the form doesnt use a previous value.
public static string product_type = ""
//In the form load event, the gridview is populated - this could be moved to anywhere.
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("DB connection string");
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Products", conn);
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sqlDataAdap.Fill(ds);
DataTable dt = new DataTable();
sqlDataAdap.Fill(dt);
conn.Close();
}
//In the cell contect click event we give it the ProductType
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//Here we declare a var to identify that we're looking at the row
var row = dataGridView1.CurrentRow.Cells;
//Now we set the string we declared earlier
product_type = Convert.ToString(row["ProductType"].Value);
//Now we repeat the grid script with a parameter which uses our string from above
SqlConnection conn = new SqlConnection("DB connection string");
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Products WHERE ProductType = @ProductType", conn);
sc.Parameters.Add("@ProductType", SqlDbType.Int).Value = product_type;
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sqlDataAdap.Fill(ds);
DataTable dt = new DataTable();
sqlDataAdap.Fill(dt);
conn.Close();
}
然后,您可以添加一个按钮,当单击该按钮将网格返回到非过滤状态时,该按钮会重复表单加载事件中的脚本
希望这有帮助