private void dataSetsToolStripMenuItem_Click(object sender, System.EventArgs e)
{
MDIChild mc = this.ActiveMdiChild as MDIChild;
if (mc == null || mc.DrawCtl == null || mc.ReportDocument == null)
return;
ToolStripMenuItem menu = sender as ToolStripMenuItem;
if (menu == null)
return;
mc.Editor.StartUndoGroup(Strings.RdlDesigner_Undo_DataSetDialog);
string dsname = menu.Text;
// Find the dataset we need
List<XmlNode> ds = new List<XmlNode>();
DesignXmlDraw draw = mc.DrawCtl;
XmlNode rNode = draw.GetReportNode();
XmlNode dsNode = draw.GetCreateNamedChildNode(rNode, "DataSets");
XmlNode dataset = null;
// find the requested dataset: the menu text equals the name of the dataset
int dsCount = 0; // count of the datasets
string onlyOneDsname = null;
foreach (XmlNode dNode in dsNode)
{
if (dNode.Name != "DataSet")
continue;
XmlAttribute nAttr = dNode.Attributes["Name"];
if (nAttr == null) // shouldn't really happen
continue;
if (dsCount == 0)
onlyOneDsname = nAttr.Value; // we keep track of 1st name;
dsCount++;
if (nAttr.Value == dsname)
dataset = dNode;
}
bool bNew = false;
if (dataset == null) // This must be the new menu item
{
dataset = draw.CreateElement(dsNode, "DataSet", null); // create empty node
bNew = true;
}
ds.Add(dataset);
using (PropertyDialog pd = new PropertyDialog(mc.DrawCtl, ds, PropertyTypeEnum.DataSets))
{
DialogResult dr = pd.ShowDialog();
if (pd.Changed || dr == DialogResult.OK)
{
if (dsCount == 1)
// if we used to just have one DataSet we may need to fix up DataRegions
// that were defaulting to that name
{
dsCount = 0;
bool bUseName = false;
foreach (XmlNode dNode in dsNode)
{
if (dNode.Name != "DataSet")
continue;
XmlAttribute nAttr = dNode.Attributes["Name"];
if (nAttr == null) // shouldn't really happen
continue;
dsCount++;
if (onlyOneDsname == nAttr.Value)
bUseName = true;
}
if (bUseName && dsCount > 1)
{
foreach (XmlNode drNode in draw.ReportNames.ReportItems)
{
switch (drNode.Name)
{
// If a DataRegion doesn't have a dataset name specified use previous one
case "Table":
case "List":
case "Matrix":
case "Chart":
XmlNode aNode = draw.GetNamedChildNode(drNode, "DataSetName");
if (aNode == null)
draw.CreateElement(drNode, "DataSetName", onlyOneDsname);
break;
default:
break;
}
}
}
}
mc.Modified = true;
}
else if (bNew) // if canceled and new DataSet get rid of temp node
{
dsNode.RemoveChild(dataset);
}
if (pd.Delete) // user must have hit a delete button for this to get set
dsNode.RemoveChild(dataset);
if (!dsNode.HasChildNodes) // If no dataset exists we remove DataSets
draw.RemoveElement(rNode, "DataSets");
mc.Editor.EndUndoGroup(pd.Changed || dr == DialogResult.OK);
}
}