Message 1 of 1
datagridview
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
hi, loading data working fine. but updating attribute is not working. can anyone help me.
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "AutoCAD Drawing (*.dwg)|*.dwg";
if (dlg.ShowDialog() == DialogResult.OK)
{
// Open the selected drawing
DocumentCollection acDocMgr = Application.DocumentManager;
acDoc = acDocMgr.Open(dlg.FileName, false);
acDb = acDoc.Database;
// Load the attribute values into the DataGridView
LoadAttributeValues();
}
}
private void LoadAttributeValues()
{
layouts = new List<Layout>();
//List<Layout> layouts = new List<Layout>();
using (acTrans = acDb.TransactionManager.StartTransaction())
{
// Get the layout dictionary
DBDictionary layoutDict = acTrans.GetObject(acDb.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
// Iterate through each layout and add it to the list
foreach (DBDictionaryEntry entry in layoutDict)
{
Layout layout = acTrans.GetObject(entry.Value, OpenMode.ForWrite) as Layout;
layouts.Add(layout);
}
acTrans.Commit();
}
// Load the attribute values into the DataGridView
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Layout");
dataTable.Columns.Add("Tag");
dataTable.Columns.Add("Value");
foreach (Layout layout in layouts)
{
using (acTrans = acDb.TransactionManager.StartTransaction())
{
BlockTableRecord acBlkTblRec = acTrans.GetObject(layout.BlockTableRecordId, OpenMode.ForRead) as BlockTableRecord;
foreach (ObjectId acObjId in acBlkTblRec)
{
Entity entity = acTrans.GetObject(acObjId, OpenMode.ForRead) as Entity;
if (entity is BlockReference)
{
BlockReference blockRef = entity as BlockReference;
foreach (ObjectId attId in blockRef.AttributeCollection)
{
AttributeReference attRef = acTrans.GetObject(attId, OpenMode.ForRead) as AttributeReference;
if (attRef != null)
{
DataRow row = dataTable.NewRow();
row["Layout"] = layout.LayoutName;
row["Tag"] = attRef.Tag;
row["Value"] = attRef.TextString;
dataTable.Rows.Add(row);
}
}
}
}
acTrans.Commit();
}
}
dataGridView1.DataSource = dataTable;
}
private void button1_Click(object sender, EventArgs e)
{
var doc = Application.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
var db = doc.Database;
using (var tr = db.TransactionManager.StartTransaction())
{
foreach (var li in _layouts)
{
if (li.AttributeValue != dataGridView1.CurrentRow.Cells[2].Value.ToString())
{
var blockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
var layoutName = li.LayoutName;
if (layoutName == "*ModelSpace*")
{
var blockTableRecord = tr.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
foreach (ObjectId objId in blockTableRecord)
{
var ent = tr.GetObject(objId, OpenMode.ForWrite) as Entity;
var attDef = ent as AttributeDefinition;
if (attDef != null && attDef.Tag == li.BlockName)
{
attDef.UpgradeOpen();
attDef.TextString = dataGridView1.CurrentRow.Cells[2].Value.ToString();
attDef.DowngradeOpen();
}
}
}
else
{
var layout = tr.GetObject(blockTable[layoutName], OpenMode.ForWrite) as Layout;
var blkTblRecId = layout.BlockTableRecordId;
var blkTblRec = tr.GetObject(blkTblRecId, OpenMode.ForWrite) as BlockTableRecord;
foreach (ObjectId objId in blkTblRec)
{
var blkRef = tr.GetObject(objId, OpenMode.ForWrite) as BlockReference;
if (blkRef != null && blkRef.Name == li.BlockName)
{
foreach (ObjectId attId in blkRef.AttributeCollection)
{
var attRef = tr.GetObject(attId, OpenMode.ForWrite) as AttributeReference;
if (attRef != null && attRef.Tag == li.BlockName)
{
attRef.UpgradeOpen();
attRef.TextString = dataGridView1.CurrentRow.Cells[2].Value.ToString();
attRef.DowngradeOpen();
}
}
}
}
}
}
}
tr.Commit();
}
ed.Regen();
}