datagrid 控件,如何点击一次某单元格,即可获取该单元格的内容

datagrid 控件,如何点击一次某单元格,即可获取该单元格的内容,第1张

前台代码:

<Grid >

<GridRowDefinitions>

<RowDefinition/>

<RowDefinition Height="25"/>

</GridRowDefinitions>

<DataGrid Name="dg" AutoGenerateColumns="True" RowHeaderWidth="0" SelectedCellsChanged="dg_SelectedCellsChanged" SelectionUnit="Cell"></DataGrid>

<TextBlock GridRow="1" Name="txt"/>

</Grid>

后台代码:

public partial class BaiduKnows : Window

{

public BaiduKnows()

{

InitializeComponent();

List<People> peoplelist = new List<People>();

peoplelistAdd(new People() { age = 10, name = "linda" });

peoplelistAdd(new People() { age = 11, name = "tom" });

peoplelistAdd(new People() { age = 12, name = "peter" });

peoplelistAdd(new People() { age = 13, name = "mary" });

dgItemsSource = peoplelist;

}

class People

{

public int age { get; set; }

public string name { get; set; }

}

private void dg_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)

{

DataGrid dg = sender as DataGrid;

People item = dgSelectedCells[0]Item as People;

string colhead = dgSelectedCells[0]ColumnHeaderToString();

switch (colhead)

{

case "age" : txtText = itemageToString();

break;

case "name": txtText = itemnameToString();

break;

}

}

}

DataGridViewCell cell = null;

if (dataGridView1SelectedCellsCount > 0)

{

cell = dataGridView1[dataGridView1SelectedCells[0]ColumnIndex, dataGridView1SelectedCells[0]RowIndex];

MessageBoxShow(cellValue == null "" : cellValueToString());

}

CellClick 事件

private void dataGridView1_Click(object sender, EventArgs e)

{

int row =dataGridView1CurrentRowIndex;

TextBox1Text=dateGridView1[0,row]ValueToString();

TextBox2Text=dateGridView1[0,row]ValueToString();

}

Button b = ecell[i]findcontrol("Id") as Button;

bText="abc";

大致是这个意思即:我们通过ecell[i]这个指定列来查找控件ID为Id的控件(即你放入的button),然后将之转换为Button即可,这个Button即为你要获取的按钮,下面的bText="abc",就是你要设置他的text属性

以上就是关于datagrid 控件,如何点击一次某单元格,即可获取该单元格的内容全部的内容,包括:datagrid 控件,如何点击一次某单元格,即可获取该单元格的内容、c#DataGridview 单击选中一行任何地方都可以获取name字段、DataGridView单击行头得到该行的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10146716.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存